I am trying to update a file in Google Drive using Javascript, but the acess_token returns null. I based my file in this question Google Drive API V3 (javascript) update file contents.
const update = () => {
gapi.load('client:auth2', () =>{
gapi.client.init({
apiKey: 'MY API KEY',
clientId: '**************.apps.googleusercontent.com',
discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"],
scope: 'https://www.googleapis.com/auth/drive',
}).then(v => {
var content = 'Hello World';
var contentBlob = new Blob([content], {
'type': 'text/plain'
});
updateFileContent('File id', contentBlob, function(response) {
console.log(response);
});
})
})
const updateFileContent = (fileId, contentBlob, callback) => {
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if (xhr.readyState != XMLHttpRequest.DONE) {
return;
}
callback(xhr.response);
};
xhr.open('PATCH', 'https://www.googleapis.com/upload/drive/v3/files/' + fileId + '?uploadType=media');
xhr.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
xhr.send(contentBlob);
}
Someone know what Im doing wrong?