2017-01-19 6 views
13

.p12キーファイルを使用してドメインワイド委任でGoogleサービスアカウントを使用できます。Googleサービスアカウントのドメインワイド委任セットは、p12の代わりにJSONキーファイルを使用します。

p12ファイルの代わりにJSONキーファイルを使用したいと思いますが、JSONキーを使用するときにサービスアカウントIDを設定する方法がわかりません。

JSONキーファイルを使用して偽装するようにユーザーを設定するにはどうすればよいですか?

作品罰金:

File p12File = new File(...); 
GoogleCredential.Builder b = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) 
.setJsonFactory(JSON_FACTORY).setServiceAccountId(properties.getServiceAccountId()) 
        .setServiceAccountPrivateKey(SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), 
         new FileInputStream(p12File), "notasecret", 
         "privatekey", "notasecret")) 
        .setServiceAccountScopes(GOOGLE_SCOPE_LIST); 
       if (properties.getServiceAccountEmail() != null) { 
       b = b.setServiceAccountUser(properties.getServiceAccountEmail()); 
       } 
       credential = b.build(); 

は動作しません:

String jsonKeyContents = "{\n" + 
      " \"type\": \"service_account\",\n" + 
      " \"project_id\": \"sxxxxxxx0\",\n" + 
      " \"private_key_id\": \"csxxxxxxxxxxxxxxxx\",\n" + 
      " \"private_key\": \"-----BEGIN PRIVATE " + 
      "KEY-----\\nMIIxxxxxxxxxxsTbwzsbw" + 
      "==\\n-----END PRIVATE KEY-----\\n\",\n" + 
      " \"client_email\": \"[email protected]-123456.iam.gserviceaccount.com\",\n" + 
      " \"client_id\": \"1111111111111111111\",\n" + 
      " \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" + 
      " \"token_uri\": \"https://accounts.google.com/o/oauth2/token\",\n" + 
      " \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" + 
      " \"client_x509_cert_url\": \"https://www.googleapis" + 
      ".com/robot/v1/metadata/x509/xxxxx%40xxxxxx-xxxxx-123456.iam.gserviceaccount.com\"\n" + 
      "}"; 
try (InputStream privateKeyInputStream = new ByteArrayInputStream(jsonKeyContents 
      .getBytes("UTF-8"))) { 
     credential = GoogleCredential.fromStream(privateKeyInputStream).createScoped(GOOGLE_SCOPE_LIST); 
} 

すべてのエンタープライズ・ユーザーを一覧表示するディレクトリのAPIを使用してJSONのキーを使用している場合、私はエラーメッセージが表示されます:

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found 
{ 
    "code" : 404, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Domain not found.", 
    "reason" : "notFound" 
    } ], 
    "message" : "Domain not found." 
} 
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146) 
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 
... 

すべての問題は、サービスアカウントの電子メールを入れる場所がないという事実に関連しているようです。

私はここで何が欠けていますか?彼らはいくつかのコードを共有することができる実際の例を持つ誰かがいますか?

答えて

9

結局のところ、私は自分自身で答えを見つけました。

はそれを修正するにはhttps://github.com/google/google-api-java-client/issues/1007#issuecomment-264157989

を参照してください:

  1. 上記指定されたJSONのキーを使用してGoogleCredential credentialFromJsonを作成します。私。 .setServiceAccountPrivateKey(credentialFromJson.getServiceAccountPrivateKey())

は、今では動作します:credential = GoogleCredential.fromStream(privateKeyInputStream).createScoped(GOOGLE_SCOPE_LIST)

  • 我々はP12ファイルからプライベートキーを作成するのではなく、P12 が、を使用したコードを使用してビルダーを作成します...このラインを使用します! \はm/

  • 2

    あなたのJSONファイルは、キー

    { 
    "type": "service_account", 
    "project_id": "your project Id", 
    "private_key_id": "private key id", 
    "private_key": "Private key", 
    "client_email": "Client email ending with .iam.gserviceaccount.com", 
    "client_id": "client -id", 
    "auth_uri": "accounts.google.com/o/oauth2/auth", 
    "token_uri": "accounts.google.com/o/oauth2/token", 
    "auth_provider_x509_cert_url": "www.googleapis.com/oauth2/v1/certs", 
    "client_x509_cert_url": "www.googleapis.com/robot/v1/metadata/x509/????? 
    -170710.iam.gserviceaccount.com" 
    } 
    

    として必要なすべてのキーと値のペアが含まれている必要があります次に、以下のコードを使用します。

    private GoogleCredential authorize1() { 
    
        GoogleCredential credential = null; 
    
        HttpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
    
        JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 
    
        try { 
    
         InputStream jsonFileStream = 
          DriveSample.class.getClassLoader().getResourceAsStream("client_secrets.json"); 
    
         GoogleCredential readJsonFile = GoogleCredential 
          .fromStream(jsonFileStream, httpTransport, JSON_FACTORY).createScoped(DriveScopes.all()); 
    
         credential = new GoogleCredential.Builder().setTransport(readJsonFile.getTransport()) 
          .setJsonFactory(readJsonFile.getJsonFactory()) 
          .setServiceAccountId(readJsonFile.getServiceAccountId()) 
          .setServiceAccountScopes(readJsonFile.getServiceAccountScopes()) 
          .setServiceAccountPrivateKey(readJsonFile.getServiceAccountPrivateKey()).build(); 
        } catch (IOException exception) { 
         exception.printStackTrace(); 
        } 
        return credential; 
    } 
    
    +0

    を、これは同じ正確な答えでありますhttps://stackoverflow.com/a/45361542/1174024 –

    +0

    Yaaはほぼ同じです。ほとんどの人が間違ったJSONファイルを取得しますが、私が初めて使ったようなすべてのキーペアが含まれていません。 –

    関連する問題