我嘗試使用谷歌的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
就是最後林e在拋出異常之前在代碼中執行? 。 –
'Fusiontables.Table.List listTables = fusiontables.table()列表();'這是在得到執行的代碼的最後一行。這會調用Credential.refreshToken。我試着直接從代碼中調用Credential.refreshToken,這也給出了相同的錯誤。 –