【问题标题】:GCP Pub/Sub throws "The Application Default Credentials are not available"GCP Pub/Sub 抛出“应用程序默认凭据不可用”
【发布时间】:2019-05-01 16:11:36
【问题描述】:

我正在尝试使用以下内容发布到 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

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

这不是 App Engine 应用程序。

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

供参考:

【问题讨论】:

  • 你在设置变量时是否使用了export,如export GOOGLE_APPLICATION_CREDENTIALS=something?如果没有,该值将在您的 shell 中设置,但不在子进程中。
  • @CharlesEngelke 是的。
  • 您可以尝试运行此命令来设置默认凭据“gcloud auth application-default login”
  • @Christopher 我的系统上没有该工具。
  • 嗨 Roy,您应该在设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 或运行命令“gcloud auth application-default login”[1] cloud.google.com/sdk 之前安装适用于 MacOS 的 SDK[1]

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


【解决方案1】:

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();

环境变量的使用要么被弃用,要么文档完全错误,或者我遗漏了一些东西,...鉴于文档质量不佳,这完全有可能。

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多