7

我正在尝试通过Google App Engine 提供的App Identity接口使用 Google Drive API。这基本上允许我的 Web 应用程序与 Google 的 API从 server 到 server进行通信。

我不需要我的用户登录,我只需要显示我自己的 Google Drive 文档。

但是,在我设置了所有适当的值和范围,并在控制台页面上启用了所有正确的 Google Drive 旋钮之后,我仍然可以通过一个简单的 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." }}

怎么了?我错过了什么?这是实际执行请求的代码 - 有趣的是,如果我使用其他 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 更改为使用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

4

5 回答 5

6

您收到的 403 错误意味着您的 GET 中没有 Authorization 标头。逻辑是,如果没有 Authorization 标头,您就是匿名的(您是军团等等 :-))。匿名使用的驱动器配额为零,因此消息。URL 缩短器具有更高的匿名配额,因此它可以工作。

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

于 2012-10-05T13:45:32.980 回答
1

AFAICT 您应该Bearer在 Authorization 标头中使用。

可能发生的情况是,Drive API 无法识别服务帐户(因为标头错误?),因此将其视为匿名请求,因为key也没有提供任何参数(请参阅common query params)。

试试这个:

connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());

或者您可以尝试将令牌添加为access_token查询参数。

于 2012-10-02T16:56:25.317 回答
1

我认为您至少应该设置一个 API 控制台条目,并在https://code.google.com/apis/console启用 Drive API 创建此条目后,您将获得一个可在您的 GoogleCredential 对象中使用的 ID。从 GoogleCredential 对象中,您可以获得访问令牌,然后您可以将其添加到您的请求中。

于 2012-10-03T15:10:48.403 回答
1

我在这里读到的(通过服务帐户的 Google 驱动器)是您使用了一种稍微不同的样式,该样式使用您从开发者控制台检索的 API KEY。
对我来说相关的部分是生成一个“服务器应用程序的密钥”,然后使用我在其他任何地方都没有读过的这种技术!

      HttpTransport httpTransport = new NetHttpTransport();
  JsonFactory jsonFactory = new JacksonFactory();
  AppIdentityCredential credential =
      new AppIdentityCredential.Builder(DriveScopes.DRIVE).build();
  // API_KEY is from the Google Console as a server API key
  GoogleClientRequestInitializer keyInitializer =
      new CommonGoogleClientRequestInitializer(API_KEY);
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential)
      .setGoogleClientRequestInitializer(keyInitializer)
      .build();
于 2014-02-05T16:19:31.340 回答
0

这个答案声称:

由于其安全模型,Drive SDK 不支持服务帐户。

如果仍然如此,一种解决方法是使用常规 Google 帐户执行一次常规 OAuth 舞蹈,并将访问和刷新令牌保留在数据存储区中。

于 2012-10-11T23:41:10.573 回答