GCP发布者/订阅者抛出“应用程序默认凭据不可用”

时间:2019-05-01 16:11:37

标签: eclipse macos authentication exception google-cloud-platform

我正尝试使用以下内容发布到Google Pub / Sub主题:

ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
Publisher publisher = null;

try {
  // Create a publisher instance with default settings bound to the topic
  publisher = Publisher.newBuilder(topicName).build();

  List<String> messages = Arrays.asList("first message", "second message");

  for (final String message : messages) {
    ByteString data = ByteString.copyFromUtf8(message);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();

    // Once published, returns a server-assigned message id (unique within the topic)
    ApiFuture<String> future = publisher.publish(pubsubMessage);

    // Add an asynchronous callback to handle success / failure
    ApiFutures.addCallback(
        future,
        new ApiFutureCallback<String>() {

          @Override
          public void onFailure(Throwable throwable) {
            if (throwable instanceof ApiException) {
              ApiException apiException = ((ApiException) throwable);
              // details on the API exception
              System.out.println(apiException.getStatusCode().getCode());
              System.out.println(apiException.isRetryable());
            }
            System.out.println("Error publishing message : " + message);
          }

          @Override
          public void onSuccess(String messageId) {
            // Once published, returns server-assigned message ids (unique within the topic)
            System.out.println(messageId);
          }
        },
        MoreExecutors.directExecutor());
  }
} finally {
  if (publisher != null) {
    // When finished with the publisher, shutdown to free up resources.
    publisher.shutdown();
    publisher.awaitTermination(1, TimeUnit.MINUTES);
  }
}

我已将您在此处看到的默认值更改为我要访问的帐户的详细信息。

环境变量指向包含pub / sub身份验证凭据的JSON文件:

GOOGLE_APPLICATION_CREDENTIALS

使用以下设置:

export GOOGLE_APPLICATION_CREDENTIALS=path/to/file.json

,并用echo $GOOGLE_APPLICATION_CREDENTIALS验证-重新启动后。

但是我仍然遇到:

The Application Default Credentials are not available. They are available
if running in Google Compute Engine. Otherwise, the environment variable 
GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining 
the credentials. See https://developers.google.com/accounts/docs/application-
default-credentials for more information.

我认为这与应用程序在其中运行的默认环境有关,或者与GCP对象认为上下文是-runningOnComputeEngine有关:

com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.

还显示一个对话框:

Unable to launch App Engine Server
Cannot determine server execution context

并且项目(Eclipse 2019-3)中没有Google Cloud Platform设置:

GCP Eclipse Settings

这不是App Engine应用程序。

  

如何设置GCP对象指向的环境-> Non App Engine。

供参考:

1 个答案:

答案 0 :(得分:0)

关于此的Google文档非常糟糕-它在任何地方都没有提及。

答案是使用:

// create a credentials provider
CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream(Constants.PUB_SUB_KEY)));

// apply credentials provider when creating publisher
publisher = Publisher.newBuilder(topicName).setCredentialsProvider(credentialsProvider).build();

不赞成使用环境变量,或者文档完全错误,或者我遗漏了某些东西,...鉴于文档不佳,这完全有可能。