【问题标题】:Google Drive API: Download file gives lockedDomainCreationFailure errorGoogle Drive API:下载文件给出了 lockedDomainCreationFailure 错误
【发布时间】:2021-06-18 17:05:54
【问题描述】:

我正在尝试使用 Google Drive File Picker 下载文件(基于此示例 https://gist.github.com/Daniel15/5994054)。文件选择器在下载文件时工作正常。它遇到 400 Bad-Request (lockedDomainCreationFailure) 错误。

代码如下:

function downloadFile(file, callback) {
  if (file.downloadUrl) {
    var accessToken = gapi.auth.getToken().access_token;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', file.downloadUrl);
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xhr.onload = function() {
      callback(xhr.responseText);
    };
    xhr.onerror = function() {
      callback(null);
    };
    xhr.send();
  } else {
    callback(null);
  }
}

这是错误信息:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "lockedDomainCreationFailure",
    "message": "The OAuth token was received in the query string, which this API forbids for response formats other than JSON or XML. If possible, try sending the OAuth token in the Authorization header instead."
   }
  ],
  "code": 400,
  "message": "The OAuth token was received in the query string, which this API forbids for response formats other than JSON or XML. If possible, try sending the OAuth token in the Authorization header instead."
 }
}

它告诉 OAuth 令牌是在查询字符串中给出的,我认为这是不正确的。 这是请求:

GET /drive/v2/files/{file-id}?key={app-key}&alt=media&source=downloadUrl HTTP/3
Host: content.googleapis.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: */*
Accept-Language: en,de;q=0.7,en-US;q=0.3
Accept-Encoding: gzip, deflate, br
Authorization: Bearer {oauth-token}
Origin: http://localhost:8800
DNT: 1
Connection: keep-alive
Referer: http://localhost:8800/
TE: Trailers

由于我使用的是 Google API 提供的下载 url,并且授权是在请求标头中给出的,所以我不知道为什么会遇到这个错误。

我很欣赏任何想法。

【问题讨论】:

  • 查看是否将请求 url 更改为 ` 'googleapis.com/drive/v2/files/[FILEID]?key=[YOUR_API_KEY] HTTP/1.1` 以及指定 Accept: application/json 是否可以解决问题。另外:{app-id} 是什么?
  • “app-id”是指应用程序密钥。我在我的OP中纠正了这一点。 解决方案不是查询content.googleapis.com,而是查询www.googleapis.com。谢谢你的提示!我不知道为什么,因为来自 Google API 的下载 URL 是第一个主机名。不需要更改“接受”标题。请求 URL 必须包含 altsource 查询参数,否则您只能获取文件元数据,而不是其内容。所以下载地址是https://www.googleapis.com/drive/v2/files/{file-id}?key={app-key}&alt=media&source=downloadUrl。再次,非常感谢你。

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


【解决方案1】:

解决方案是将主机 content.googleapis.com(从 Google API 作为下载 URL 提供)更改为 www.googleapis.com。感谢ziganotschka 的提示!

所以正确的下载地址是https://www.googleapis.com/drive/v2/files/{file-id}?key={app-key}&alt=media&source=downloadUrl。它必须包含“alt”和“source”查询参数,否则您只能获得文件元数据,而不是其内容。无需更改“接受”标题。

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    相关资源
    最近更新 更多