【问题标题】:Google API credentials not working as expectedGoogle API 凭据未按预期工作
【发布时间】:2016-03-16 12:09:00
【问题描述】:

我正在尝试为我的项目获取 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

任何建议都非常感谢,在此先感谢。我应该错过什么?

【问题讨论】:

  • 有解决办法吗?
  • 请参阅下面的答案,如果它适合您,请投票赞成:)

标签: java api gdata


【解决方案1】:

我终于找到了解决办法,

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

希望对其他人有所帮助。

问候, 维沙尔 Z。

【讨论】:

  • 您不必设置ServiceAccountUser 吗?没有它它还能工作吗?
  • 如果没有.setServiceAccountUser("admin@domain.com"),我的代码会抛出“未授权访问此资源/api”错误,如果没有它,它会抛出 invalid_grant 错误......
猜你喜欢
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
相关资源
最近更新 更多