即使我在请求中发送身份验证令牌,Google Drive API V3“需要登录”

时间:2021-07-05 13:00:17

标签: javascript google-drive-api google-oauth

所以即使在发送身份验证令牌时我也会收到 401 错误,这是我的代码

var file = document.getElementById('input-file').files[0];
var metadata = {
      'mimeType': file.type,
    'name': name, // Filename at Google Drive
    'parents': ['0AN75N3P23eTJUk9PVA'], // Folder ID at Google Drive
};

var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('supportsAllDrives',true);
form.append('auth',accessToken);
form.append('file', file);

var xhr = new XMLHttpRequest();
xhr.open('post', 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = 'json';
xhr.onload = () => {
    console.log(xhr.response.id); // Retrieve uploaded file ID.
};
xhr.send(form);

提前致谢

注意:

  • 查询在上传到我的驱动器时有效,但是当尝试上传到该共享驱动器时发生错误

0 个答案:

没有答案