Google API凭据未按预期运行

时间:2015-12-10 18:16:56

标签: java api gdata

我正在尝试为我的项目获取GoogleCredential,如下所示,

GoogleCredential credential = new GoogleCredential.Builder()
                     .setTransport(httpTransport)
                     .setJsonFactory(jsonFactory)
                     .setServiceAccountId("xxx@famous-store-115417.iam.gserviceaccount.com")
                     .setServiceAccountScopes(SCOPES)
                     .setServiceAccountPrivateKeyFromP12File(new File(p12))
                     .build();

但我收到了错误,

Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
    at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
    at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
    at com.google.gdata.client.Service.getFeed(Service.java:1017)
    at ImportAwards.openFeed(ImportAwards.java:49)
    at ImportAwards.importAwards(ImportAwards.java:62)
    at ImportAwards.main(ImportAwards.java:29)
Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:258)
    ... 7 more

我知道它的权限相关错误,但我使用了正确的密钥和服务帐户ID,我也通过网络搜索了它,但它与建议的解决方案相同。我也审核了this

任何建议都表示赞赏,提前谢谢。我应该缺少什么?

1 个答案:

答案 0 :(得分:0)

最后我发现解决方案是,

我使用的是Goodle Drive API,因此我需要与服务帐户(xxx@famous-store-115417.iam.gserviceaccount.com)共享该文档。之后它正常运作。

我希望对别人有帮助。

此致 Vishal Z。