【发布时间】: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。
供参考:
- Server to Server(错误消息中的链接)
- Publish
Java 7 应用程序
- Mac OS(塞拉利昂)
- 设置了应用程序可以读取文件的文件权限。
【问题讨论】:
-
你在设置变量时是否使用了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