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 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:
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
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.
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 ...
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 .
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With