【问题标题】:Google Drive API returns 403 while uploading a fileGoogle Drive API 在上传文件时返回 403
【发布时间】:2019-03-28 15:02:52
【问题描述】:

这是登录后将文件上传到 Google 潜水的代码。

var accessToken = this.userData.token; // Here gapi is used for retrieving the access token.
console.log("accessToken :: " + accessToken);
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
form.append('file', file);

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

如果我使用用于生成 client_id 和 app_key 的帐户,它可以发布文件数据,但如果我使用任何其他帐户,则会出现如下错误。

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id 403

我使用相同的详细信息检查了 oAuth plaground,并得到了这样的响应。

Www-authenticate: Bearer realm="https://accounts.google.com/", error=insufficient_scope, scope="https://www.googleapis.com/auth/drive"
{
  "error": {
    "code": 403, 
    "message": "Insufficient Permission", 
    "errors": [
      {
        "domain": "global", 
        "message": "Insufficient Permission", 
        "reason": "insufficientPermissions"
      }
    ]
  }
}

【问题讨论】:

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


    【解决方案1】:
    {
      "error": {
        "code": 403, 
        "message": "Insufficient Permission", 
        "errors": [
          {
            "domain": "global", 
            "message": "Insufficient Permission", 
            "reason": "insufficientPermissions"
          }
        ]
      }
    }
    

    与客户端 ID 无关。当用户运行您的应用程序时,系统会提示他们登录 google 并同意您的应用程序访问他们的 Google 驱动器帐户。您将获得一个 accessToken,授予您访问其驱动器帐户的权限。

    Insufficient Permission 表示您拥有的 accessToken 无权执行您正在尝试执行的操作。我会检查您尝试上传此文件的位置。

    • 确认您上传的是当前经过身份验证的用户驱动器帐户,而不是第一个用户的驱动器帐户?

    • 我看不到您是如何对用户进行身份验证的,请确保您请求的是写入范围之一,也可能是您请求了错误的范围。

    【讨论】:

    • 嗨,我使用以下链接登录并获取访问令牌,并且我使用相同的令牌 (this.userData.token) 在标头中进行授权。我没有使用任何范围。
    • 另外,为什么我需要 file.ge()?我只是上传文件。
    • 我的错误我以为你在哪里更新文件本身。我没有意识到这是一个新文件。我会仔细检查您用于验证此用户的范围。
    猜你喜欢
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    相关资源
    最近更新 更多