0

App/Computeで実行されていないアプリケーションのサービスアカウントキーをJavaでGoogle Cloud SDKでプログラムで作成しようとしていますエンジン。 This questionは鉱山と似ていますが、App Engine上で実行されているため、App Engine APIのクラスを使用するのと同じコードを使用することはできません。Google Cloud Java SDKのダウンロードなしでサービスアカウントを認証する方法(App Engineではなく)

関連コードは以下のとおりです。私の問題はAppIdentityCredentialがAppEngine APIの一部であるため、ここでは使用できないということです。代わりにパラメータとして渡すことはできますか?新しいBuilder()メソッドの3番目のパラメータにはHttpRequestInitializerが入りますが、このインターフェイスの実装がわかればわかりません。助けてください。

import com.google.api.services.iam.v1.Iam; 
import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest; 
import com.google.api.services.iam.v1.model.ServiceAccountKey; 

AppIdentityCredential credential = new AppIdentityCredential(
      Arrays.asList("https://www.googleapis.com/auth/cloud-platform")); 
Iam iam = new Iam.Builder(httpTransport, JSON_FACTORY,credential) 
     .setApplicationName(APPLICATION_NAME).build(); 
ServiceAccountKey key = iam.projects().serviceAccounts().keys() 
    .create(SERVICE_ACCOUNT_RESOURCE_NAME, new CreateServiceAccountKeyRequest()).execute(); 

答えて

1

あなたは、アプリケーションが実行されている環境に基づいて認証情報を取得するために同じコードを使用できるようになりますApplication Default Credentialsを使用することができます。

たとえば、システムで開発しているときにgcloud account credentialsを使用することができます。コードがGoogle Compute EngineまたはGoogle App Engineで実行されている場合、コードはAPIの認証に関連するサービスアカウントの資格情報を自動的に使用します。代わりに、JSONファイルから資格情報をロードする必要がある場合は、GOOGLE_APPLICATION_CREDENTIALS環境変数を使用してそれを上書きすることもできます。

ここに、既存のサービスアカウントの新しいキーを作成して印刷する実例を示します。

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.services.iam.v1.Iam; 
import com.google.api.services.iam.v1.IamScopes; 
import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest; 
import com.google.api.services.iam.v1.model.ServiceAccountKey; 
import java.io.IOException; 
import java.security.GeneralSecurityException; 
import java.util.ArrayList; 
import java.util.List; 

public class IamDemo { 
    /** Name of the application. */ 
    private static final String APPLICATION_NAME = "IamDemoJava"; 

    /** Project Name. */ 
    private static final String PROJECT_NAME = "MY_PROJECT_NAME"; 

    /** Name of the service account to create a new key for. */ 
    private static final String SERVICE_ACCOUNT_NAME = "dummy-sa"; 

    /** Full email address of the service account. */ 
    private static final String SERVICE_ACCOUNT_EMAIL = 
     SERVICE_ACCOUNT_NAME + "@" + PROJECT_NAME + ".iam.gserviceaccount.com"; 

    /** Full service account resource string expected by the IAM API. */ 
    private static final String SERVICE_ACCOUNT_RESOURCE_NAME = 
     "projects/" + PROJECT_NAME + "/serviceAccounts/" + SERVICE_ACCOUNT_EMAIL; 

    /** Global instance of the HTTP transport. */ 
    private static HttpTransport httpTransport; 

    /** Global instance of the JSON factory. */ 
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 

    public static void main() throws IOException, GeneralSecurityException { 
    Iam iam = initIam(); 
    ServiceAccountKey key = createServiceAccountKey(iam); 

    // Print the key 
    System.out.println(key.toString()); 
    } 

    private static Iam initIam() throws IOException, GeneralSecurityException { 
    httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 

    // Authenticate using Google Application Default Credentials. 
    GoogleCredential credential = GoogleCredential.getApplicationDefault(); 


    if (credential.createScopedRequired()) { 
     List<String> scopes = new ArrayList<>(); 
     // Enable full Cloud Platform scope. 
     scopes.add(IamScopes.CLOUD_PLATFORM); 
     credential = credential.createScoped(scopes); 
    } 

    // Create IAM API object associated with the authenticated transport. 
    return new Iam.Builder(httpTransport, JSON_FACTORY, credential) 
     .setApplicationName(APPLICATION_NAME) 
     .build(); 
    } 

    private static ServiceAccountKey createServiceAccountKey(Iam iam) 
     throws IOException, GeneralSecurityException { 
    CreateServiceAccountKeyRequest request = new CreateServiceAccountKeyRequest(); 

    // Customize the request parameters if needed 

    return iam.projects() 
     .serviceAccounts() 
     .keys() 
     .create(SERVICE_ACCOUNT_RESOURCE_NAME, request) 
     .execute(); 
    } 
} 
関連する問題