我正在尝试使用官方 Java 示例熟悉 Google Drive API。然而,在浪费了几个小时并尝试设置示例两次之后,我仍然无法按预期使用它。
它没有显示文件的内容,而是在 FileServlet(/svc 路径)中抛出 404 错误。更具体地说,service.files().get(fileId).execute();
似乎返回 null。我尝试过使用不同的文件、不同的 MIME 类型,直接从 Google 云端硬盘以及使用 Google 文件选择器。
我已按照 https://developers.google.com/drive/examples/java 中描述的步骤进行操作尽可能接近。
有没有人能够运行示例?
编辑:
这是请求 URL /svc?file_id=0B08R9MrOE-ejZTY2M2I5MjAtYmVjZS00OTkyLWI4ZTEtOTg4OGM3YTIxMWEw
时 FileServlet 的日志输出(第 78 行抛出 404 错误):
2012-04-26 08:21:36.077
com.google.api.client.http.HttpRequest execute: -------------- REQUEST --------------
GET https://www.googleapis.com/drive/v1/files/0B08R9MrOE-ejZTY2M2I5MjAtYmVjZS00OTkyLWI4ZTEtOTg4OGM3YTIxMWEw
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.8.3-beta (gzip)
D 2012-04-26 08:21:36.263
com.google.api.client.http.HttpResponse <init>: -------------- RESPONSE --------------
403 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
date: Thu
date: 26 Apr 2012 06:21:36 GMT
expires: Thu
expires: 26 Apr 2012 06:21:36 GMT
cache-control: private
cache-control: max-age=0
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
content-length: 188
server: GSE
x-google-cache-control: remote-fetch
via: HTTP/1.1 GWA
D 2012-04-26 08:21:36.265
com.google.api.client.http.HttpResponse getContent: Response size: 188 bytes
D 2012-04-26 08:21:36.271
com.google.api.client.http.HttpResponse getContent: {
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit Exceeded. Please sign up",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit Exceeded. Please sign up"
}
}
这听起来很像“我不认识你”,所以我又看了看我的客户端 ID 和客户端密码:我注意到有两个客户端 ID 和客户端密码显示在API 控制台,一个“Web 应用程序的客户端 ID”和另一个“Drive SDK 的客户端 ID”。我以前使用过用于 Web 应用程序的那个,所以我尝试切换到用于 Drive SDK 的那个。不幸的是,这并没有改变任何东西。同样的错误...:/
最佳答案
根据您的日志,您的 404 错误似乎是由对 Drive API 的请求返回的 403 错误引起的。
来自 Google API 的错误通常在其正文中包含解释。在你的情况下:
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit Exceeded. Please sign up",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit Exceeded. Please sign up"
}
}
这通常意味着您没有在您的 Google API 控制台项目上启用对 Google Drive API 的访问。为了做到这一点:
- 在 Google APIs Console 中打开您的项目
- 转到服务部分
- 通过点击开/关开关启用Drive SDK和Drive API
- 不要忘记在 Drive SDK 部分配置您的应用,并在“**API 访问”部分配置您的 OAuth 设置
这在 Get started > Register an App 中都有描述。我们文档的一部分。您还应确保阅读了入门 的其他子部分。
关于java - Google Drive SDK - Java 示例不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10323579/