我很难让Google Cloud Dataflow Java SDK与PubSub源一起使用。下面的简约代码总是返回401(未授权)错误。我试图弄清楚如何提供凭据,但找不到让它工作的方法。我事先使用了gcloud auth登录来确保我已登录并且订阅存在。
抛出以下错误:
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project my-dataflow: An exception occured while executing the Java class. null: InvocationTargetException: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
[ERROR] {
[ERROR] "code" : 401,
[ERROR] "errors" : [ {
[ERROR] "domain" : "global",
[ERROR] "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
[ERROR] "reason" : "unauthorized"
[ERROR] } ],
[ERROR] "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
[ERROR] "status" : "UNAUTHENTICATED"
[ERROR] }
我的代码如下:
public class MyDataflow{
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.create();
PubsubOptions dataflowOptions = options.as(PubsubOptions.class);
dataflowOptions.setStreaming(true);
Pipeline p = Pipeline.create(options);
p.apply(PubsubIO.<String>read().subscription("projects/my-project/subscription/my-subscription")
.withCoder(StringUtf8Coder.of()));
// Run the pipeline.
p.run().waitUntilFinish();
}
}
答案 0 :(得分:1)
我必须将GOOGLE_APPLICATION_CREDENTIALS设置为指向我的json凭据文件。
https://developers.google.com/identity/protocols/application-default-credentials