【发布时间】:2012-10-02 13:55:47
【问题描述】:
我正在尝试通过 Google App Engine 提供的 App Identity 接口使用 Google Drive API。这基本上允许我的 Web 应用程序与 Google 的 API from server to server 进行通信。
我不需要我的用户登录,我只需要显示我自己的 Google Drive 文档。
但是,在我设置了所有适当的值和范围,并在控制台页面上启用了所有正确的 Google Drive 旋钮后,我仍然可以通过对 https://www.googleapis.com/drive/v2/files 的简单 GET 请求得到这个:
{ "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." }}
怎么了?我错过了什么?这是实际执行请求的代码 - 有趣的是,如果我使用其他 API(例如 URL 缩短器 API),它会很好地工作:
var scopes = new java.util.ArrayList();
scopes.add("https://www.googleapis.com/auth/drive");
var appIdentity = AppIdentityServiceFactory.getAppIdentityService();
var accessToken = appIdentity.getAccessToken(scopes);
var url = new URL("https://www.googleapis.com/drive/v2/files");
var connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "OAuth " + accessToken.getAccessToken());
编辑
例如,如果我只是将 API 更改为使用 urlshortner API,它就可以工作:
var url = new URL("https://www.googleapis.com/urlshortener/v1/url/history");
然后输出:
{ "kind": "urlshortener#urlHistory", "totalItems": 0, "itemsPerPage": 30}
那么肯定有什么东西不能与 Google Drive 和 App Identity 一起使用?
编辑 2
我从这里的正确答案中找到了一些帮助:https://stackoverflow.com/a/12526286/50394
但它谈论的是在 Google Apps 上设置客户端 API 范围,而且我没有使用 Google Apps,我只是使用 Google App Engine 的域foo.appspot.com
【问题讨论】:
-
这是 Java 吗?与
var?您可能希望使用 Google 提供的 OAuth/API 库,这将消除一些用户错误的机会。 code.google.com/p/google-api-java-client -
Hrm,但这是一个简单的 REST 调用。我不想仅仅为了一个 REST 调用而使用一个完整的库。是的,它仍然是 JVM(Rhino)。但我可以使用相同的 API。
-
addRequestProperty添加标题而不是查询参数,对吗?也许尝试记录您返回的访问令牌并手动尝试使用该令牌的请求? -
您不应该在授权标头中使用“Bearer”而不是 OAuth 吗?
-
@JasonHall 我编辑的问题表明,如果我使用不同的 API(例如 urlshortener),相同的代码也可以工作。
标签: java google-app-engine google-api google-drive-api