【发布时间】:2020-07-24 08:22:04
【问题描述】:
Google Drive API 有时会返回带有内容的 HTML 页面的响应:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Sorry...</title>
<style>
body {
font-family: verdana, arial, sans-serif;
background-color: #fff;
color: #000;
}
</style>
</head>
<body>
<div>
<table>
<tr>
<td><b>
<font face=sans-serif size=10>
<font color=#4285f4>G</font>
<font color=#ea4335>o</font>
<font color=#fbbc05>o</font>
<font color=#4285f4>g</font>
<font color=#34a853>l</font>
<font color=#ea4335>e</font>
</font>
</b></td>
<td style="text-align: left; vertical-align: bottom; padding-bottom: 15px; width: 50%">
<div style="border-bottom: 1px solid #dfdfdf;">Sorry...</div>
</td>
</tr>
</table>
</div>
<div style="margin-left: 4em;">
<h1>We\'re sorry...</h1>
<p>... but your computer or network may be sending automated queries. To protect our users, we can\'t process
your request right now.</p>
</div>
<div style="margin-left: 4em;">See <a href="https://support.google.com/websearch/answer/86640">Google Help</a> for
more information.<br /><br /></div>
<div style="text-align: center; border-top: 1px solid #dfdfdf;"><a href="https://www.google.com">Google Home</a>
</div>
</body>
</html>
当使用参数“?alt=media”调用https://www.googleapis.com/drive/v3/files/file_id 端点以获取文件内容时,会发生这种情况。
我没有超出任何配额。
我在 https://www.googleapis.com/drive/v3/files?q=trashed = False 等其他调用中没有收到此错误。
错误发生在我用 Vala (https://github.com/bcedu/VGrive) 编写的应用程序中。此应用程序运行良好,没有出现此错误。当 Google 更改身份验证时,它开始发生,而不是在查询参数中使用访问令牌,而是使用 HTTP 标头。我进行了此更改,并在某些请求中解决了问题(例如我提到的搜索文件),但是当我想下载文件时仍然出现错误。
我正在使用 Google Drive api v3
我正在使用 vala 库 Soup(一个著名的 Gnome 库 https://developer.gnome.org/libsoup/unstable/)来提出请求。您可以使用以下代码测试错误:
public static int main(string[] args) {
// THIS DOESNT WORKS
string method = "GET";
string file_id = ""; // Some Google Drive File ID
string uri = "https://www.googleapis.com/drive/v3/files/%s?alt=media".printf(file_id);
string access_token = ""; // A valid acces_token
Soup.Session session = new Soup.Session ();
Soup.Message message = new Soup.Message (method, uri);
message.request_headers.append("Authorization", "Bearer %s".printf(access_token));
session.send_message (message);
// Response is stored in message.response_body.data
print("AUTOMATED QUERY ERROR:\n"+(string)message.response_body.data+"\n\n");
// THIS WORKS
uri = "https://www.googleapis.com/drive/v3/files?q=trashed = False and 'root' in parents";
session = new Soup.Session ();
message = new Soup.Message (method, uri);
message.request_headers.append("Authorization", "Bearer %s".printf(access_token));
session.send_message (message);
// Response is stored in message.response_body.data
print("THIS WORKS, SO TOKEN IS PASSES CORRECTLLY...\n"+(string)message.response_body.data+"\n");
return 0;
}
运行测试:
valac --pkg libsoup-2.4 GoogleTest.vala
./GoogleTest
您需要安装:
- libsoup-2.4
- valac
我希望得到一个成功的响应或一个身份验证错误,但不是一个说我正在进行自动查询的 html 页面。它是一个 API,它应该被应用程序使用,而不仅仅是人类。
我是否遗漏了 google drive API 中的某些内容?我是否需要做一个额外的步骤才能通过 Soup 传递不记名令牌?
【问题讨论】:
-
您是一直收到此错误还是偶尔收到此错误?你能检查一下这个错误是否只发生在大文件上吗?您是否尝试按块下载文件?我怀疑您在没有官方库帮助的情况下执行所有 HTTP 请求,也许尝试转到 OAuth Playground 并检查您是否在正确范围内执行所有正确请求。
-
我总是收到这个错误,无论文件大小。当你调用 api 来下载一个文件时,它已经给了你块,问题是我没有得到第一个块,我得到一个错误。 Vala 没有 Google Drive API 库,所以我必须阅读文档并制作自己的库,是的,我发出了整个 HTTP 请求。
-
您能否在 OAuth Playground 或
curl中获取该 HTTP 请求?这似乎应该可以正常工作,可能是Soup库的问题。 -
我试过用 curl 发出同样的请求,结果是一样的:
curl -H "Authorization: Bearer the_acces_token" "https://www.googleapis.com/drive/v3/files/file_id?alt=media" -
我已经向 google 问题跟踪器打开了一个问题,因为这似乎是 api issuetracker.google.com/issues/153717392 的问题
标签: google-drive-api google-authentication vala bearer-token