2012-08-22 95 views
1

我嘗試使用谷歌的Fusion Tables API從Java。我得到400 Bad Request"error" : "invalid_grant"谷歌融合表400 invalid_grant

我使用的服務帳戶的API和OAuth2.0的授權我的要求。

1)我已經下載了我的祕密密鑰,並用下面的代碼片段的憑據。

GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(HTTP_TRANSPORT) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId("[[[email protected]account.com]]") 


.setServiceAccountPrivateKeyFromP12File(KeyResources.INSTANCE.getFileFromPath("b9b48154142d235bbd711c3eb8f86bb2ec155faf-privatekey.p12")) 
    .setServiceAccountScopes("oauth2:https://www.googleapis.com/auth/fusiontables") 
    .build(); 

2)我使用下面的代碼連接到代碼片段。

Credential credential = ServiceAccount.INSTANCE.getCredentials(); 
     Fusiontables fusiontables = new Fusiontables.Builder(ServiceAccount.HTTP_TRANSPORT, ServiceAccount.JSON_FACTORY, credential) 


.setApplicationName("sfdgdfgdfgdfgdfgdfgf345q23") 
     .setHttpRequestInitializer(credential) 
     .build(); 

    Fusiontables.Table.List listTables = fusiontables.table().list(); 

這是我得到的錯誤。

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request 
{ 
    "error" : "invalid_grant" 
} 
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103) 
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303) 
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323) 
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:340) 
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:508) 

任何幫助將是偉大的。

感謝, Ganesh神V

+0

就是最後林e在拋出異常之前在代碼中執行? 。 –

+0

'Fusiontables.Table.List listTables = fusiontables.table()列表();'這是在得到執行的代碼的最後一行。這會調用Credential.refreshToken。我試着直接從代碼中調用Credential.refreshToken,這也給出了相同的錯誤。 –

回答

1

我認爲你必須刪除 「[]」 在你的服務帳戶ID。

除此之外,如果你得到403未經授權錯誤您更改服務帳戶ID之後,它可能是因爲你還沒有授予你的表給你服務帳戶的編輯權限。要更改訪問權限,只需將您的融合表分享給您的服務帳戶電子郵件並授予「可編輯」權限。

希望它可以幫助你。

0

我想你需要設置用戶的電子郵件,作爲服務帳戶需要委派的用戶帳戶:

GoogleCredential credential = new GoogleCredential.Builder() 
      ... 
      .setServiceAccountUser(UserEmail) 

,並確保服務Fusion Tables的API上,並在用戶的主人域控制檯授予正確的範圍,以您的服務帳戶:

https://www.googleapis.com/auth/fusiontables