서비스 계정이있는 Google Cloud Storage API에 액세스하려고하면 invalid_grant 오류가 발생합니다. 다음 코드를 잘못 이해할 수 없습니다. 어떤 생각? Google Storage API를 사용하는 첫 번째 애플리케이션입니다. 어떤 도움이라도 대단히 감사하겠습니다.Google Cloud Storage 및 서비스 계정에 잘못된 오류가 있습니다.
소스 코드
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)
으흠 ... 잘못된 것이 없습니다. 귀하의 서비스 계정은 "@ developer.gserviceaccount.com"으로 끝나는 긴 이메일 주소처럼 보이십니까? 그리고 "key.p12"가 그런 식으로 열릴 수있는 적절한 장소에 있습니까? 명령 줄 샘플 앱을 사용하여 자격 증명이 올바른지 확인해 보는 것이 좋습니다. https://code.google.com/p/google-api-java-client/source/browse/plus-serviceaccount-cmdline-sample/? repo = 샘플 & r = 1dd135aa1dfa0b5d326ba2a0c795ed18cd052495 –
감사. 실제로 이메일 주소 (@ developer.gserviceaccount.com으로 끝남) 대신 클라이언트 ID (.apps.googleusercontent.com으로 끝남)를 사용하고있었습니다. –