2013-08-28 176 views
3

当我尝试使用服务帐户访问Google Cloud Storage API时,出现invalid_grant错误。我无法弄清楚下面的代码有什么问题。任何想法 ?这是我使用Google Storage API的第一个应用程序。任何帮助将不胜感激。Google云端存储和服务帐户出现invalid_grant错误

源代码

public static void main(String[] args) throws Exception { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JsonFactory jsonFactory = new JacksonFactory(); 

    List<String> scopes = new ArrayList<String>(); 
    scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL); 

    Credential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(jsonFactory) 
      .setServiceAccountId("XXXX") 
      .setServiceAccountPrivateKeyFromP12File(new File("key.p12")) 
      .setServiceAccountScopes(scopes).build(); 

    Storage storage = new Storage.Builder(httpTransport, jsonFactory, 
      credential).setApplicationName("test") 
      .build(); 

    List<String> list = new ArrayList<String>(); 
    List<Bucket> buckets = storage.buckets().list("XXXX").execute().getItems(); 
    if(buckets != null) { 
     for(Bucket b : buckets) { 
      list.add(b.getName()); 
     } 
    } 
} 

错误和堆栈跟踪

Exception in thread "main" 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:269) 
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) 
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) 
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) 
at GoogleStorageFetcher.main(GoogleStorageFetcher.java:143) 
+2

槽糕......我看不出有什么错。您的服务帐户看起来像一个很长的电子邮件地址,以“@ developer.gserviceaccount.com”结尾,对吧?和“key.p12”是在这样打开的正确的地方?可能值得尝试命令行示例应用程序以确保您的凭据有效:https://code.google.com/p/google-api-java-client/source/browse/plus-serviceaccount-cmdline-sample/? repo = samples&r = 1dd135aa1dfa0b5d326ba2a0c795ed18cd052495 –

+0

谢谢。实际上,我使用的是客户端ID(以.apps.googleusercontent.com结尾)而不是电子邮件地址(以@ developer.gserviceaccount.com结尾)。 –

回答

22

此错误是由于不正确的服务帐户ID。我使用的是客户端ID(以.apps.googleusercontent.com结尾)而不是电子邮件地址(以@ developer.gserviceaccount.com结尾)。电子邮件地址没有问题。

+0

我犯了同样的错误。 Thx分享! –

+0

您是否知道@ iam.gserviceaccount.com是否也是有效的电子邮件地址?当我创建一个服务帐户时,它总是给我一个格式的电子邮件地址。 – Sardonic

9

我知道这个问题是从一年前。

我有此错误:错误: “invalid_grant”,说明: “” 乌里: “”

对我来说,问题解决了同步与NTP服务器的时钟。我的服务器的防火墙阻塞了一个端口,服务器的时间没有正确更新。

欲了解更多信息,visit this Google page.

+1

在我的电脑时间与现实不同步时出现同样的问题。谷歌身份验证给了这个错误。 –

+0

thx很多,这固定了我的一个大问题 – mgiza

+0

你们是如何解决同步?我的机器在/etc/ntp.conf上已经有了ubuntu的时区。所以这意味着我的问题可能与此无关? – Sardonic

相关问题