我正嘗試使用Google Cloud SDK在Java中以編程方式創建服務帳戶密鑰,以便在App/Compute上運行的應用程序發動機。 This question與我的類似,但它在App引擎上運行,所以我不能使用相同的代碼,因爲它使用App Engine API中的類。如何在不下載Google雲Java SDK的情況下對服務帳戶進行身份驗證(不在App Engine上)
相關代碼如下。我的問題是AppIdentityCredential是AppEngine API的一部分,因此無法在此處使用。我可以通過參數傳遞什麼?新的Builder()方法中的第三個參數需要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();