我尝试用Google Drive文件拾取器下载一个文件(基于这个例子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."
}
}
它告诉我们在查询字符串中给出了To 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,并且授权是在请求头中给出的,所以我不知道为什么会遇到这个错误。
我很感激你的建议。
2条答案
按热度按时间azpvetkf1#
解决方案是将主机
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”查询参数,否则只能得到文件 meta数据,而不能得到其内容。不需要更改“Accept”头。nwo49xxi2#
#Google云端硬盘Api第3版更新
https://www.googleapis.com/drive/v3/files/{file-id}?key={APIKey}&alt=media&source=downloadUrl
如何获取/创建API密钥〉https://console.cloud.google.com/apis/credentials