2013-08-28 5 views
3

서비스 계정이있는 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) 
+2

으흠 ... 잘못된 것이 없습니다. 귀하의 서비스 계정은 "@ developer.gserviceaccount.com"으로 끝나는 긴 이메일 주소처럼 보이십니까? 그리고 "key.p12"가 그런 식으로 열릴 수있는 적절한 장소에 있습니까? 명령 줄 샘플 앱을 사용하여 자격 증명이 올바른지 확인해 보는 것이 좋습니다. https://code.google.com/p/google-api-java-client/source/browse/plus-serviceaccount-cmdline-sample/? repo = 샘플 & r = 1dd135aa1dfa0b5d326ba2a0c795ed18cd052495 –

+0

감사. 실제로 이메일 주소 (@ developer.gserviceaccount.com으로 끝남) 대신 클라이언트 ID (.apps.googleusercontent.com으로 끝남)를 사용하고있었습니다. –

답변

22

이 오류는 잘못된 서비스 계정 ID 때문. (@ developer.gserviceaccount.com으로 끝나는) 이메일 주소 대신 클라이언트 ID (.apps.googleusercontent.com으로 끝남)를 사용하고있었습니다. 이메일 주소에는 문제가 없습니다.

+0

나는 똑같은 실수를했다. 공유 Thx! –

+0

@ iam.gserviceaccount.com도 유효한 이메일 주소인지 알고 있습니까? 서비스 계정을 만들 때 항상 형식의 전자 메일 주소를 제공합니다. – Sardonic

9

나는이 질문이 1 년 전부터 나온 것임을 알고있다.

는이 오류가 있었다 : 오류 : "invalid_grant을"설명 : ""열린 우리당을 ""내 경우

는 문제가 NTP로 서버 시계를 동기화 해결되었다. 내 서버의 방화벽이 포트를 차단하고 서버의 시간이 올바르게 업데이트되지 않았습니다.

자세한 내용은 visit this Google page.

+1

내 컴퓨터의 시간이 현실과 맞지 않을 때 같은 문제가 발생했습니다. google 인증 기관에서이 오류를 발견했습니다. –

+0

많이 thx, 나를 위해 큰 문제를 해결했습니다 – mgiza

+0

어떻게 너희들이 동기화를 수정 했습니까 ?? 내 컴퓨터는 이미 /etc/ntp.conf에 우분투의 시간대를 가지고 있습니다. 그래서 내 문제가 이것과 관련이 없다는 것을 의미합니까? – Sardonic

관련 문제