簡體   English   中英

GCP Pub / Sub拋出“應用程序默認憑據不可用”

[英]GCP Pub/Sub throws “The Application Default Credentials are not available”

我正在嘗試使用以下內容發布到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):

GCP Eclipse設置

這不是App Engine應用程序。

如何設置GCP對象指向的環境 - >非App Engine。

以供參考:

谷歌關於此的文件非常糟糕 - 它在任何地方都沒有提及。

答案是使用:

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

環境變量的使用要么已被棄用,要么文檔錯誤,或者我遺漏了一些東西,......鑒於文檔很差,這完全是可能的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM