Rishabh Rastogi
Rishabh Rastogi

Reputation: 11

GCP Pub/Sub | Publish Messages to a regional endpoint

I am using spring-boot based app to publish messages to a GCP topic. I want to leverage the message ordering and hence I want to publish the messages to a regional endpoint.

I have enabled the message ordering using spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true and using spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 to publish messages to a specific region.

However, I am getting the following error while publishing.

com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

However, when I do not use spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 and publish messages to a global endpoint instead, there is no exception.

What am I missing here?

Dependencies:

<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>
<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>

<dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>spring-cloud-gcp-dependencies</artifactId>
   <version>3.5.0</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
 </dependencies>
</dependencyManagement>

Upvotes: 0

Views: 529

Answers (1)

Rishabh Rastogi
Rishabh Rastogi

Reputation: 11

I have found the solution while referring to other threads and GitHub issues.

The credentials provider does not have the necessary scopes to publish a message to a regional endpoint. We need to add the necessary scopes explicitly. See below.

  @Bean
  public CredentialsProvider credentialsProvider() {
    return () ->
        ServiceAccountCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFilePath)))
            .createScoped(PublisherStubSettings.getDefaultServiceScopes());
  }

Reference

  1. Google Pub/Sub API gets OAUTH2 authentication failure when trying to connect to a specific endpoint
  2. https://github.com/googleapis/java-pubsub/issues/930

Upvotes: 0

Related Questions