如何在代码中明确指向我的服务帐户文件

How to explicitly point my service account file in code

在存储、BigQuery 等其他 Google 服务中,您可以在 JAVA 代码中定义要使用的服务帐户:

// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

使用 Google Secret Manager 似乎不可能。为什么? 唯一的办法是在VM上设置一个环境变量?

我尝试按照建议使用 Credentials Provider

 GoogleCredentials credentials = ServiceAccountCredentials.fromStream(credentialsInputStream);
     CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);
     SecretManagerServiceSettings settings = SecretManagerServiceSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
     client = SecretManagerServiceClient.create(settings);

但是没用

Caused by: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
    at io.grpc.Metadata$Key.validateName(Metadata.java:742)
    at io.grpc.Metadata$Key.<init>(Metadata.java:750)
    at io.grpc.Metadata$Key.<init>(Metadata.java:668)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:959)
    at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:954)
    at io.grpc.Metadata$Key.of(Metadata.java:705)
    at io.grpc.Metadata$Key.of(Metadata.java:701)
    at com.google.api.gax.grpc.GrpcHeaderInterceptor.<init>(GrpcHeaderInterceptor.java:60)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:239)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access00(InstantiatingGrpcChannelProvider.java:71)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:210)
    at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:217)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:200)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:156)
    at com.google.cloud.secretmanager.v1.stub.GrpcSecretManagerServiceStub.create(GrpcSecretManagerServiceStub.java:237)
    at com.google.cloud.secretmanager.v1.stub.SecretManagerServiceStubSettings.createStub(SecretManagerServiceStubSettings.java:226)
    at com.google.cloud.secretmanager.v1.SecretManagerServiceClient.<init>(SecretManagerServiceClient.java:154)
    at com.google.cloud.secretmanager.v1.SecretManagerServiceClient.create(SecretManagerServiceClient.java:135)

因为 class com.google.cloud.secretmanager.v1.SecretManagerServiceClient

中的异常
/**
   * Constructs an instance of SecretManagerServiceClient, using the given settings. This is
   * protected so that it is easy to make a subclass, but otherwise, the static factory methods
   * should be preferred.
   */
  protected SecretManagerServiceClient(SecretManagerServiceSettings settings) throws IOException {
    this.settings = settings;
    this.stub = ((SecretManagerServiceStubSettings) settings.getStubSettings()).createStub();
  }

要自定义凭据,您可以创建自定义 secretManagerServiceSettings:

SecretManagerServiceSettings secretManagerServiceSettings =
    SecretManagerServiceSettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
        .build();

SecretManagerServiceClient client =
    SecretManagerServiceClient.create(secretManagerServiceSettings);