Google API getSignedUrl responseDisposition does nothing - html

Using Google API on NodeJS I do something like
const [url] = await mybucket.file('myfiles/uniquefileid').getSignedUrl({
action: 'write',
contentType: 'image/png',
expires: dayjs().add(1, 'hour').toDate(),
responseDisposition: 'attachment; filename="myfile.png"',
version: 'v4'
});
Within the returned URL I see ..&response-content-disposition=attachment%3B%20filename%3D%27file.png%27
So it's doing the right thing there. Then I upload the file on the front end via Axios. Now when I retrieve this file using an HTML component like <a href="fullpath" download /> it still downloads with the unique file id instead of the filename. I inspect the headers of the file (I open it in a new tab) and I don't see any Content-Disposition on the response header. What am I doing wrong? Do I need to configure Gcloud to use this header somewhere?

Related

Is it possible to update a json file on the host server from Flutter app using http.post?

I am trying to update a json file on the server with http module in Flutter, but I cannot. I want to modify the content of the json file which I have placed on the public_html folder in the server. Is it a good idea to use http.post for such cases, and if it is yes why the code below does not work then?
return http.post(url, body: jsonEncode(body), headers: {'Content-type': 'application/json'}
).then((http.Response response)

Application/Json error when sending Gmail with Postman

I'm trying to send a Gmail with Postman but got this error:
These are my configured headers:
I have configured everything needed on Google Cloud engine (Oauth permissions, certifications...etc), read similar posts, and also tried testing from gmail API playground and everything was ok:
Any idea of what could be wrong?
Remove all custome headers , (Create a new request by following below steps)
Steps:
https://console.developers.google.com/ Goto url and create an API. (by clicking credentials>create credentials)
https://console.developers.google.com/apis/api/gmail.googleapis.com/overview Goto url, click credentials> create new 0auth0.2 ( select applicaiton type as Web application)
While creating 0auth0.2 scroll down and set Authorized redirect URIs as https://localhost
CLick the download as json and then save the auth
Open postman and set authorization as 0auth2.0 from authorization header:
Generate new token by filling the content as per the downloaded json ( open the json and fill postman as in json)
set scope and state as : https://mail.google.com/ ( full access ) see scope at : https://developers.google.com/gmail/api/auth/scopes
In postman Set url as :https://gmail.googleapis.com/gmail/v1/users/praveendvd0pravu%40gmail.com/messages/send?key={YOURAPI_KEY_That_was_generated}
goto https://www.base64encode.org/ , and paste below content and click encode. Copy the output.
From: youremail#gmail.com
To: tosomeemail#gmail.com
Subject: Hi
Hi this is
9): paste it as json body
{
"raw":"IEZyb206IHlvdXJlbWFpbEBnbWFpbC5jb20KIFRvOiB0b3NvbWVlbWFpbEBnbWFpbC5jb20KIFN1YmplY3Q6IEhpICAKICAgIApIaSB0aGlzIGlzIA=="
}
Send it :

use link to download json file

<a href="www.myfile.json" download>download file</a>
"www.myfile.json" is a link to a json file. My goal is when user clicks "download file", the json
file is downloaded automatically. However, with above code, when I click "download file", it opens
the json file in browswer for me. I would like to know what I am doing wrong here.
I have tried the solution in this link(Chrome 65 blocks cross-origin <a download>. Client-side workaround to force download?), but I got Access to fetch at 'XXXXX' from origin 'XXXX' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. However, I do set 'Access-Control-Allow-Origin':'*' in my request header. I am not able to modify anything on server side. What should be the right way to do for me?
My header looks like below:
headers: new Headers({
'Origin': location.origin,
'Access-Control-Allow-Origin': '*',
}),
mode: 'cors'
})
Besides, I also tried set mode: 'no-cors', but it does not work.
The Access-Control-Allow-Origin: * header needs to be in the response, not the request. You won't be able to read the response data without cooperation from the server.
So find a more cooperative server.
There's a simple CORS proxy that will download any URL you give it, tack on the Access-Control-Allow-Origin header, and send it back to you. If you're comfortable with a third party seeing the contents of your JSON file, you can change the fetch line in the linked solution from
fetch(url, {
to
fetch(`https://bypasscors.herokuapp.com/api/?url=${encodeURIComponent(url)}`, {

Google Drive API append file?

How do I append content to the end of a file in Google Drive using the API ?
Do I really have to download the whole thing, then edit the local copy, and then re-upload the whole thing again?
Yes you really have to download edit the file and upload it again. There is no way to programmatically edit a file. Except maybe a spreadsheet but then you would be using the Google sheets API and not the Google drive API.
You can use the drive apis resumable upload with some restrictions:
(I don't quite remember if its true) Minimum bytes uploaded have to be 262144 except for the final upload which "creates" the file, which can contain less
An upload-session expires after one week,
you can set Content-Range to */* if you don't know the final filesize
The file wont show up in google drive in the ui until complete,Upload using a similiar header to the example below, where /* is the final byte length of the file. It has to be one byte less, like in the example below: 262146/262147
I recommend getting a service account for gcp project, you can create a folder in your personal drive and share it with the service account email.
To save some time, because the drive api documentation is not the best, here in "pure" python http requests:
First you have to create the file and get the session_url:
headers = {"Authorization": "Bearer "+myAccesstoken,
"Content-Type": "application/json"}
file_metadata = {
'name': "myFile.txt",
'mimeType': "text/plain",
'parents': [myFolderid],
"uploadType": "resumable"
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
headers=headers,
data=json.dumps(file_metadata)
)
session_url=r.headers['Location']
Then you can upload data to it:
headers = {
"Authorization": "Bearer "+myAccesstoken,
"Content-Range": 'bytes 0-262144/*'}
if is_final_data:
headers = {
"Authorization": "Bearer "+myAccestoken,
"Content-Range": 'bytes 262144-262146/262147'}
sd = io.BytesIO()
sd.write(bytes("Wurst", "ASCII"))
sd.seek(0)
r = requests.put(
session_url,
headers=headers,
data=sd
)
To get last uploaded byte position, if you are resuming an upload, send an empty put request only with the session url and authorization headers and read its response headers afterwards.
You can store the session url in a file and resume upload for one week.
Note: You will need something like below, since the access token is only valid for a limited amount of time.
if credentials.access_token_expired:
credentials.refresh(httplib2.Http())

How to retrieve data in AngularJS using $http from a link without a extension (like .json, etc.)?

I am trying to retrieve data from the following link:
https://npropendata.rdw.nl/parkingdata/v2/
Opening the link you will notice that it is in a form of a JSON file. I confirm that the data is correct as I copied the data and saved as a .json file locally and was able to use the $http request to retrieve the data.
Now, how do I retrieve this automatically without having to save the file every time manually, as there is no .json extension in the link? Basically I tried:
app.controller('MyCtrl', function($scope,$http){
$scope.entries = [];
$http.get('https://npropendata.rdw.nl/parkingdata/v2/').success(function(data) {
// do something
});
});
But that does not work. Copying the data and saving it as a json file and referring it as follows does work:
app.controller('MyCtrl', function($scope,$http){
$scope.entries = [];
$http.get('savedfilename.json').success(function(data) {
// do something
});
});
You have to add following headers to your api service to enable CORS.
Access-Control-Allow-Origin: [the same origin from the request]
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: [the same ACCESS-CONTROL-REQUEST-HEADERS from request]