首页 > 解决方案 > DataFlow SDK 2.x:PubSubIO 授权错误

问题描述

使用 Dataflow Java SDK 2 从 Pubsub 读取

我正在尝试如下:

public class App2 {
    public static void main(String[] args) {
        Pipeline pipeline = Pipeline.create(PipelineOptionsFactory.fromArgs(args).create());
        pipeline
                .apply("ReadStrinsFromPubsub",
                        PubsubIO.readStrings().fromTopic("projects/dev/topics/trading"))
                .apply("PrintToStdout", ParDo.of(new DoFn<String, Void>() {
                    @ProcessElement
                    public void processElement(ProcessContext c) {
                        System.out.printf("Received at %s : %s\n", Instant.now(), c.element()); // debug log
                    }
                }));

        pipeline.run().waitUntilFinish();
    }
}

发生错误的原因是:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "The request is missing a valid API key.",
    "reason" : "forbidden"
  } ],
  "message" : "The request is missing a valid API key.",
  "status" : "PERMISSION_DENIED"
}

我该如何解决?

标签: javagoogle-cloud-platformgoogle-cloud-pubsubdataflow

解决方案


gcloud auth application-default login将使您能够使用登录机器上的默认服务帐户。


推荐阅读