Google Drive API, files.update:
https://developers.google.com/drive/api/v3/reference/files/update
This API is giving CORS error, the strange thing is in the same drive app that I'm creating, on the same domain (I'm testing it directly online):
- files.create API runs OK
- files.update API gives me CORS error (preflight request by browser can't find allow origin header in response)
The documentation on developers.google.com doesn't mention about CORS issue, what could be the problem that one API is ok while the other is not?
The 2 functions to create and update are here but shouldn't be a code issue, since CORS is about the origin of script while the functions are on the same page:
// Create new file in Gdrive
// See: https://developers.google.com/drive/api/v3/reference/files/create
// See: https://tanaikech.github.io/2018/08/13/upload-files-to-google-drive-using-javascript
async function gdrive_create_file(Folder_Id,File_Name,Binobj){
var Metadata = {
"name": File_Name, // Filename at Google Drive
"mimeType": "text/plain", // mimeType at Google Drive
"parents": [Folder_Id], // Folder ID at Google Drive
};
// Here gapi is used for retrieving the access token.
var Access_Token = gapi.auth.getToken().access_token;
var Form = new FormData();
Form.append("metadata", new Blob([JSON.stringify(Metadata)], {type: 'application/json'}));
Form.append("file", Binobj);
// Make request to Gdrive
var [Lock,Unlock] = new_lock();
var File_Id = null;
var Xhr = new XMLHttpRequest();
// Gdrive to return id as indicated in 'fields=id'
Xhr.open(
"post",
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id"
);
Xhr.setRequestHeader("Authorization", "Bearer "+Access_Token);
Xhr.responseType = "json";
Xhr.onload = ()=>{
log("[Dad's TE] Gdrive file id:",Xhr.response.id); // Retrieve uploaded file ID.
File_Id = Xhr.response.id;
if (File_Id==null)
alert("Failed to create file in Gdrive!");
Unlock();
};
Xhr.send(Form);
// Wait to get resulting file id
await Lock;
return File_Id;
}
// Update a file in Gdrive
log("DEBUG HERE: 0");
async function gdrive_update_file(File_Id,File_Name,Binobj){
var Metadata = {
"name": File_Name, // Filename at Google Drive
"mimeType": "text/plain" // mimeType at Google Drive
};
// Here gapi is used for retrieving the access token.
var Access_Token = gapi.auth.getToken().access_token;
var Form = new FormData();
Form.append("metadata", new Blob([JSON.stringify(Metadata)], {type: 'application/json'}));
Form.append("file", Binobj);
// Make request to Gdrive
var [Lock,Unlock] = new_lock();
var Xhr = new XMLHttpRequest();
// Gdrive to return id as indicated in 'fields=id'
Xhr.open(
"patch",
`https://www.googleapis.com/upload/drive/v3/files/${File_Id}?uploadType=multipart&fields=id`
);
Xhr.setRequestHeader("Authorization", "Bearer "+Access_Token);
Xhr.responseType = "json";
Xhr.onload = ()=>{
log("[Dad's TE] Gdrive file id:",Xhr.response.id); // Retrieve uploaded file ID.
File_Id = Xhr.response.id;
if (File_Id==null)
alert("Failed to update file in Gdrive!");
Unlock();
};
alert("DEV ERROR: See CORS error in console log!");
Xhr.send(Form);
// Wait to get resulting file id
await Lock;
return File_Id;
}
The new_lock function:
function new_lock(){
var Unlock,Lock = new Promise((Res,Rej)=>{ Unlock=Res; });
return [Lock,Unlock];
}
Aucun commentaire:
Enregistrer un commentaire