首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Drive API:下载文件出现lockedDomainCreationFailure错误

Google Drive API:下载文件出现lockedDomainCreationFailure错误
EN

Stack Overflow用户
提问于 2021-06-17 17:32:47
回答 1查看 191关注 0票数 1

我正在尝试使用Google Drive file Picker (基于这个示例https://gist.github.com/Daniel15/5994054)下载一个文件。文件选取器在下载文件时工作得很好。它会遇到400错误请求(lockedDomainCreationFailure)错误。

代码如下:

代码语言:javascript
复制
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);
  }
}

以下是错误消息:

代码语言:javascript
复制
{
 "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."
 }
}

它告诉我们to OAuth令牌是在查询字符串中给定的,我认为这不是真的。下面是请求:

代码语言:javascript
复制
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

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

我很欣赏你的想法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-18 17:05:55

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

因此,正确的下载url是https://www.googleapis.com/drive/v2/files/{file-id}?key={app-key}&alt=media&source=downloadUrl。它必须包含"alt“和"source”查询参数,否则您只能获取文件元数据,而不能获取其内容。不需要更改"Accept“头。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68016649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档