Google Cloud Platform pub/sub Publisher,如何提供默认应用程序凭据以外的凭据

Red*_*ift 4 java google-cloud-platform google-cloud-pubsub

使用com.google.cloud.google-cloud库(http://googlecloudplatform.github.io/google-cloud-java/0.21.1/index.html),我有以下Google Cloud Platform发布的代码/子:

    TopicName topicName = TopicName.create("projectId...", "topic...");
    Publisher pub = Publisher.defaultBuilder(topicName).build();
Run Code Online (Sandbox Code Playgroud)

如何提供发布者使用的凭据?我已将它们放在内存中,因为它们是通过其他方式配置而不是将它们硬编码到文件中.

使用自定义凭据的所有示例如下:

Storage storage = StorageOptions.newBuilder()
    .setProjectId(PROJECT_ID)
    .setCredentials(GoogleCredentials.fromStream(
    new FileInputStream(PATH_TO_JSON_KEY))).build();
Bucket bucket = storage.create(BucketInfo.of("myBucketName"));
Run Code Online (Sandbox Code Playgroud)

但是没有PubSubOptions或PublisherOptions类来编写类似的代码(我将FileInputStream替换为ByteArrayInputStream).

这是一个有相同问题,但代码示例不起作用的人:https://github.com/GoogleCloudPlatform/google-cloud-java/issues/1751

Jon*_*eet 7

您可以在构建器上设置凭据提供程序:

GoogleCredentials credentials = GoogleCredentials.fromStream(
    new FileInputStream(PATH_TO_JSON_KEY)));
Publisher pub = Publisher
    .defaultBuilder(topicName)
    .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
    .build();
Run Code Online (Sandbox Code Playgroud)

现在,这将设置CallSettings使用...但我认为它不会设置在设置频道本身时使用的凭据.(看起来Java gRPC代码和我更熟悉的C#代码之间可能存在差异.)