Google Drive API通过Google App Engine

Luc*_*eis 7 java google-app-engine google-api google-drive-api

我正在尝试通过Google App Engine提供的App Identity界面使用Google Drive API .这基本上允许我的Web应用程序从服务器到服务器与Google的API进行通信.

我不需要我的用户登录,我只需要显示自己的Google云端硬盘文档.

但是,在我设置了所有适当的值和范围,并在控制台页面上启用了所有正确的Google云端硬盘旋钮后,我仍然可以通过以下简单的GET请求获取https://www.googleapis.com/drive/v2/files:

{ "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." }}
Run Code Online (Sandbox Code Playgroud)

怎么了?我错过了什么?这是实际执行请求的代码 - 有趣的是,如果我使用其他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());
Run Code Online (Sandbox Code Playgroud)

编辑

如果我只是更改API以使用urlshortnerAPI,例如,它可以工作:

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

pin*_*yid 6

您获得的403错误意味着您的GET中没有Authorization标头.逻辑是没有授权标题,你是匿名的(你是军团等等等等):-)).匿名使用的Drive配额为零,因此消息.URL缩短器具有更高的匿名配额,因此它可以正常工作.

我建议您将URL更改为指向您自己的http服务器,并检查您实际发送的标头.