Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCP PubSub: Publish message via CURL type of request

Does anyone have a working example of how to publish a message to a GCP PubSub topic via CURL type of commands, directly from shell?

I'm trying to not use the CLI and not use the client libraries, and I'm getting hung up on the OAUTH stuff.

I'd be great to have a bullet list of the things a bash script running on Linux would need to do, if anyone has such or can cobble one together I'd appreciate it very much.

Items I already have:

  • I have a Linux compute engine I'm working on
  • I've got working examples of publishing via CLI and Python
  • I've got a service account which is working nicely with pubsub (see previous bullet)

I fully recognize Google recommends using the CLI or API Client Libraries, but I need to be able to run this on a host with minimal installations (no CLI, no python libraries, etc.).

I think I need to do the following:

  1. base64 encode my data
  2. create a JSON Web Tokens (JWT)
  3. use the JWS to get a OAUTH token
  4. use token to call the API - e.g. POST https://pubsub.googleapis.com/v1/projects/myproject/topics/mytopic:publish

Ideas appreciated and thank you very much...Rich

Reference links:

https://cloud.google.com/pubsub/docs/publisher#pubsub-publish-message-protocol https://groups.google.com/forum/#!topic/cloud-pubsub-discuss/8fGaG5cWiTk https://groups.google.com/forum/?hl=sw#!topic/cloud-pubsub-discuss/8fGaG5cWiTk https://developers.google.com/identity/protocols/OAuth2WebServer https://developers.google.com/identity/protocols/OAuth2ServiceAccount

like image 518
Rich Murnane Avatar asked Jan 11 '18 15:01

Rich Murnane


People also ask

How to integrate Spring Cloud GCP PubSub with Google pub/sub?

The first thing I need to tell you is that Spring Cloud GCP PubSub provides us with an interface called PubSubOperations to work with Google Pub/Sub. The implementation of this interface is the PubSubTemplate class and behind the sense, this PubSubTemplate class also uses the Google PubSub client library to work with Google Pub/Sub.

How do I publish a pub/sub message?

Using ordering keys 1 In the Cloud Console, go to the Pub/Sub topics page.#N#Go to the Pub/Sub topics page 2 Click the topic ID. 3 In the Topic details page, click Publish messages. 4 In the Message body field, enter the message data. 5 In the Ordering key field, enter an ordering key. 6 Click Publish. More ...

What is the response to the pub/sub C++ API?

If the request is successful, the response is a JSON object with the message ID. The following example is a response with a message ID: Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries . For more information, see the Pub/Sub C++ API reference documentation .

What is Google pub/sub and how to use it?

Google Pub/Sub is one of the cloud messaging services that we can use if your application has features that require using messaging technology. In this article, I will show you how to publish a message to Google Pub/Sub topic using the Pub/Sub Java client library!


1 Answers

Yes, it is possible to publish with the curl command. You would need to pass in the access token into the request. To get it, log into the desired service account that you will use for publishing. If you haven't created one, you can do so and limit to only allow publishing:

gcloud iam service-accounts create curl-publisher

gcloud projects add-iam-policy-binding <project with topic> --member=serviceAccount:curl-publisher@<project with created service account>.iam.gserviceaccount.com --role=roles/pubsub.publisher

Now, you can log into the service account from the command line and get the access token:

gcloud auth login curl-publisher@<project with created service account>.iam.gserviceaccount.com

gcloud auth application-default print-access-token

You only need to run these above steps once in order to get the access token. Now, you can use that access token in a curl command:

PROJECT=my-project
TOPIC=my-topic
ACCESS_TOKEN=<token printed out above>
curl -H 'content-type: application/json' -H "Authorization: Bearer $ACCESS_TOKEN" -X POST --data $'{  "messages": [{"data": "abcd"}]}'  https://pubsub.googleapis.com/v1/projects/$PROJECT/topics/$TOPIC:publish
like image 96
Kamal Aboul-Hosn Avatar answered Nov 09 '22 08:11

Kamal Aboul-Hosn