如何在不下载Google Cloud Java SDK的情况下验证服务帐户(不在App Engine上)

如何在不下载Google Cloud Java SDK的情况下验证服务帐户(不在App Engine上),java,google-app-engine,google-cloud-platform,google-oauth-java-client,google-iam,Java,Google App Engine,Google Cloud Platform,Google Oauth Java Client,Google Iam,我正试图用Java和Google Cloud SDK以编程方式为一个不在App/Compute引擎上运行的应用程序创建一个服务帐户密钥。与我的类似,但它在App engine上运行,因此我不能使用与它使用App engine API中的类相同的代码 相关代码如下。我的问题是AppIdentityCredential是AppEngine API的一部分,因此不能在这里使用。我可以将什么作为参数传入?new Builder()方法中的第三个参数接受,但我不知道应该传入这个接口的什么实现。感谢您的帮助

我正试图用Java和Google Cloud SDK以编程方式为一个不在App/Compute引擎上运行的应用程序创建一个服务帐户密钥。与我的类似,但它在App engine上运行,因此我不能使用与它使用App engine API中的类相同的代码

相关代码如下。我的问题是AppIdentityCredential是AppEngine API的一部分,因此不能在这里使用。我可以将什么作为参数传入?new Builder()方法中的第三个参数接受,但我不知道应该传入这个接口的什么实现。感谢您的帮助

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();
您可以使用,这将允许您根据应用程序运行的环境使用相同的代码获取凭据

例如,它允许您在系统上开发时使用。当代码在Google Compute Engine或Google App Engine上运行时,代码将在API中自动使用关联的服务帐户凭据进行身份验证。如果需要从JSON文件加载凭据,还可以使用
GOOGLE\u APPLICATION\u 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();
  }
}
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.jackson2.JacksonFactory;
导入com.google.api.services.iam.v1.iam;
导入com.google.api.services.iam.v1.IamScopes;
导入com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest;
导入com.google.api.services.iam.v1.model.ServiceAccountKey;
导入java.io.IOException;
导入java.security.GeneralSecurityException;
导入java.util.ArrayList;
导入java.util.List;
公开课演示{
/**应用程序的名称*/
私有静态最终字符串应用程序\u NAME=“IamDemoJava”;
/**项目名称*/
私有静态最终字符串PROJECT\u NAME=“MY\u PROJECT\u NAME”;
/**要为其创建新密钥的服务帐户的名称*/
私有静态最终字符串服务\u ACCOUNT\u NAME=“dummy sa”;
/**服务帐户的完整电子邮件地址*/
专用静态最终字符串服务\u帐户\u电子邮件=
服务账户名称+“@”+项目名称+“.iam.gserviceaccount.com”;
/**IAM API所需的完整服务帐户资源字符串*/
私有静态最终字符串服务\帐户\资源\名称=
“项目/”+项目名称+“/serviceAccounts/”+服务帐户电子邮件;
/**HTTP传输的全局实例*/
专用静态HttpTransport-HttpTransport;
/**JSON工厂的全局实例*/
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
public static void main()引发IOException、GeneralSecurityException{
Iam Iam=initIam();
ServiceAccountKey=createServiceAccountKey(iam);
//打印钥匙
System.out.println(key.toString());
}
私有静态Iam initIam()引发IOException、GeneralSecurityException{
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
//使用Google应用程序默认凭据进行身份验证。
GoogleCredential credential=GoogleCredential.getApplicationDefault();
if(credential.createScopedRequired()){
列表范围=新的ArrayList();
//启用完整的云平台范围。
scopes.add(IamScopes.CLOUD\u平台);
凭证=凭证。创建作用域(作用域);
}
//创建与经过身份验证的传输关联的IAM API对象。
返回新的Iam.Builder(httpTransport、JSON_工厂、凭证)
.setApplicationName(应用程序名称)
.build();
}
专用静态ServiceAccountKey createServiceAccountKey(Iam Iam)
抛出IOException,GeneralSecurityException{
CreateServiceAccountKeyRequest=新建CreateServiceAccountKeyRequest();
//如果需要,自定义请求参数
返回iam.projects()
.serviceAccounts()
.keys()
.创建(服务\帐户\资源\名称、请求)
.execute();
}
}