Matthew
Matthew

Reputation: 125

DataFlow SDK 2.x: PubSubIO authorization error

Reading from Pubsub using Dataflow Java SDK 2

I'm trying like below:

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

An error occurs caused by:

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"
}

How can I solve it?

Upvotes: 1

Views: 424

Answers (2)

rompetroll
rompetroll

Reputation: 4799

gcloud auth application-default login will enable you to use the default service account from the machine you login from.

Upvotes: 1

Ryan Yuan
Ryan Yuan

Reputation: 2566

You may be missing the service account set up for your local machine to access GCP.

Please refer to Getting Started with Authentication to set up your service account and the GOOGLE_APPLICATION_CREDENTIALS variable.

1) Create a service account in the GCP console;

2) Download the json key file to your local;

3) Set the GOOGLE_APPLICATION_CREDENTIALS variable to the path of the json key file.

Upvotes: 0

Related Questions