微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Google Drive API:下载文件出现 lockedDomainCreationFailure 错误

我正在尝试使用 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,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 并且授权是在请求标头中给出的,所以我不知道为什么会遇到这个错误

我感谢任何想法。

解决方法

解决方案是将主机 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”查询参数,否则您只能获取文件元数据,而不能获取其内容。无需更改“接受”标题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?