Google 驱动器 API:下载文件出现 lockedDomainCreationFailure 错误
Google Drive API: Download file gives lockedDomainCreationFailure error
我正在尝试使用 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 并且在请求 header 中给出了授权,所以我没有任何线索为什么我 运行 陷入这个错误。
我很感激任何想法。
解决方案是将主机 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”查询参数,否则你只能得到文件元数据,而不是它的内容。无需更改“接受”header.
我正在尝试使用 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 并且在请求 header 中给出了授权,所以我没有任何线索为什么我 运行 陷入这个错误。
我很感激任何想法。
解决方案是将主机 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”查询参数,否则你只能得到文件元数据,而不是它的内容。无需更改“接受”header.