Authenticating pub/sub REST publish API

6,665 views
Skip to first unread message

Shushu Inbar

unread,
Sep 20, 2016, 11:04:57 AM9/20/16
to Google Cloud Pub/Sub Discussions
Hi all,
I am new to GCP and pub/sub, so maybe I am missing something basic.
I followed the getting started instructions, created topic and subscription, and I was able to use the API explorer to send messages to the new pubsub I created.
Still, when using curl from command line, I get 401 error.

The credentials I am trying to use are taken from here:


This is the request I am sending:
curl -XPOST 'https://pubsub.googleapis.com/v1/projects/test-hbase-spark/topics/events:publish?key={KEY}' -d'{
 "messages": [{"event":"interaction_loaded","sessionId":"unknown","properties":{"defaultDesign":"1","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","interactionId":"57dff415e6e75e19532dfbe4","voicrType":"multi poll two","publisherId":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"},"metadata":{"browser":"chrome","os":"linux","device":"unknown","browser_version":"51.0.2704.106","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","current_url":"http://renderer.qmerce.com/interaction/57dff415e6e75e19532dfbe4","screen_height":"1080","screen_width":"1920","language":"en-US","interaction_id":"57dff415e6e75e19532dfbe4","voicr_type":"multi poll two","publisher_id":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"}}]}'




The response is:
{
  "error": {
    "code": 401,
    "message": "The request does not have valid authentication credentials.",
    "status": "UNAUTHENTICATED"
  }
}


Please advise,
Shushu

Antonio Fornié Casarrubios

unread,
Jan 3, 2017, 10:29:19 AM1/3/17
to Google Cloud Pub/Sub Discussions
https://developers.google.com/api-client-library/javascript/features/authentication

I found it. The reason is the key is enough only when invoking something public, otherwise you must also give the OAuth Id (or perhaps something else instead). The previous link explains it 

Antonio Fornié Casarrubios

unread,
Jan 3, 2017, 10:29:19 AM1/3/17
to Google Cloud Pub/Sub Discussions
Hi. Exactly the same problem here. Did anybody find a solution?

Thanks, cheers
anto


El martes, 20 de septiembre de 2016, 17:04:57 (UTC+2), Shushu Inbar escribió:

Kir Titievsky

unread,
Jan 3, 2017, 1:55:34 PM1/3/17
to Antonio Fornié Casarrubios, Google Cloud Pub/Sub Discussions
Many thanks for posting your solution, Antonio.

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-pubsub-discuss/d0ed630b-02db-49e5-b728-cf75c3a077f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Kir Titievsky | Product Manager | Google Cloud Pub/Sub

Alex (Cloud Platform Support)

unread,
Jan 10, 2017, 11:35:42 AM1/10/17
to Google Cloud Pub/Sub Discussions

Hi Shushu,


Did Antonio’s solution helped you in resolving your error? Or would you still be looking for advise on it?

Hector C Maldonado

unread,
Mar 30, 2017, 3:41:05 PM3/30/17
to Google Cloud Pub/Sub Discussions
Greetings,
I just joined this group. Fairly knew to GCP. I am working on a pubsub project. I am able to publish json formatted file to a pubsub topic and have the data reflected on front-end service URL. now I am wanting to do the same thing without the python script, instead using  curl. I have my service account and key in a file on my local. I am not sure how to assemble the curl command to do the post. I am using the following command but getting the 401 error mentioned already

curl -XPOST 'https://pubsub.googleapis.com/v1/projects/hd-engineering/topics/valet_crud_api_pub:publish?key={@hd-engineering-db0af71d7d0d.json}' also
curl -d @cashstar.data https://pubsub.googleapis.com/v1/projects/hd-engineering/topics/valet_crud_api_pub:publish?key=@hd-engineering-db0af71d7d0d.json Here i am passing the data
i want to publish. Any feedback would be greatly appreciated. At this point i have exhausted all references and now need community help.

Kir Titievsky

unread,
Mar 30, 2017, 3:59:46 PM3/30/17
to Hector C Maldonado, Google Cloud Pub/Sub Discussions
Hector, Do you think the the Cloud Client libraries or the gcloud CLI would work for you? They make authentication (and many other things) simpler. 

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-pubsub-discuss/f9167f2d-e0b5-4f39-9744-76955efdf2f8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Balu Ganesh

unread,
Oct 4, 2017, 8:50:51 AM10/4/17
to Google Cloud Pub/Sub Discussions
Hi, 
Could you able to push the data directly using the REST url given in the publisher guide? How did you do the authentication? Please help. 

Kir Titievsky

unread,
Oct 4, 2017, 9:45:09 AM10/4/17
to Balu Ganesh, Google Cloud Pub/Sub Discussions
Balu, could you tell us a bit more about why pushing directly to the REST end point is important? Perhaps we could find another solutition.  

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-pubsub-discuss/a7c9af10-4abc-4c92-9c4d-64402a889791%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kir Titievsky

unread,
Oct 4, 2017, 3:18:10 PM10/4/17
to Balu Ganesh, Google Cloud Pub/Sub Discussions
Also: check out the Google API explorer -- a handy way to experiment with our REST APIs. Here's the page for Pub/Sub publish: https://developers.google.com/apis-explorer/#search/pubsub/pubsub/v1/pubsub.projects.topics.publish?

On Wed, Oct 4, 2017 at 9:44 AM, Kir Titievsky <k...@google.com> wrote:
Balu, could you tell us a bit more about why pushing directly to the REST end point is important? Perhaps we could find another solutition.  

On Wed, Oct 4, 2017 at 8:50 AM Balu Ganesh <balu.gan...@gmail.com> wrote:
Hi, 
Could you able to push the data directly using the REST url given in the publisher guide? How did you do the authentication? Please help. 


On Tuesday, 20 September 2016 20:34:57 UTC+5:30, Shushu Inbar wrote:
Hi all,
I am new to GCP and pub/sub, so maybe I am missing something basic.
I followed the getting started instructions, created topic and subscription, and I was able to use the API explorer to send messages to the new pubsub I created.
Still, when using curl from command line, I get 401 error.

The credentials I am trying to use are taken from here:


This is the request I am sending:
curl -XPOST 'https://pubsub.googleapis.com/v1/projects/test-hbase-spark/topics/events:publish?key={KEY}' -d'{
 "messages": [{"event":"interaction_loaded","sessionId":"unknown","properties":{"defaultDesign":"1","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","interactionId":"57dff415e6e75e19532dfbe4","voicrType":"multi poll two","publisherId":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"},"metadata":{"browser":"chrome","os":"linux","device":"unknown","browser_version":"51.0.2704.106","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","current_url":"http://renderer.qmerce.com/interaction/57dff415e6e75e19532dfbe4","screen_height":"1080","screen_width":"1920","language":"en-US","interaction_id":"57dff415e6e75e19532dfbe4","voicr_type":"multi poll two","publisher_id":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"}}]}'




The response is:
{
  "error": {
    "code": 401,
    "message": "The request does not have valid authentication credentials.",
    "status": "UNAUTHENTICATED"
  }
}


Please advise,
Shushu

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsub...@googlegroups.com.
--

Kir Titievsky | Product Manager | Google Cloud Pub/Sub

Dan Cowell

unread,
Nov 16, 2017, 8:29:34 AM11/16/17
to Google Cloud Pub/Sub Discussions
I wound up here after spending considerable time looking for the same information.

I also need to publish directly to the REST endpoint, because I am attempting to use Pub/Sub on a platform where none of the client libraries are supported and I do not have access to install or run a CLI tool.

Publishing to the REST endpoint is the only option.

It seems strange that the protocol format is documented in the docs, but there's no information about how to handle auth.


On Wednesday, October 4, 2017 at 8:45:09 PM UTC+7, Kir Titievsky wrote:
Balu, could you tell us a bit more about why pushing directly to the REST end point is important? Perhaps we could find another solutition.  

On Wed, Oct 4, 2017 at 8:50 AM Balu Ganesh <balu.gan...@gmail.com> wrote:
Hi, 
Could you able to push the data directly using the REST url given in the publisher guide? How did you do the authentication? Please help. 


On Tuesday, 20 September 2016 20:34:57 UTC+5:30, Shushu Inbar wrote:
Hi all,
I am new to GCP and pub/sub, so maybe I am missing something basic.
I followed the getting started instructions, created topic and subscription, and I was able to use the API explorer to send messages to the new pubsub I created.
Still, when using curl from command line, I get 401 error.

The credentials I am trying to use are taken from here:


This is the request I am sending:
curl -XPOST 'https://pubsub.googleapis.com/v1/projects/test-hbase-spark/topics/events:publish?key={KEY}' -d'{
 "messages": [{"event":"interaction_loaded","sessionId":"unknown","properties":{"defaultDesign":"1","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","interactionId":"57dff415e6e75e19532dfbe4","voicrType":"multi poll two","publisherId":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"},"metadata":{"browser":"chrome","os":"linux","device":"unknown","browser_version":"51.0.2704.106","referrer":"http://www.telegraph.co.uk/news/2016/09/19/cuddling-kittens-can-kill-you-warn-scientists/","current_url":"http://renderer.qmerce.com/interaction/57dff415e6e75e19532dfbe4","screen_height":"1080","screen_width":"1920","language":"en-US","interaction_id":"57dff415e6e75e19532dfbe4","voicr_type":"multi poll two","publisher_id":"54c7a9b285e2f62843b07317","creatorId":"5671441e671d2abb43f62095"}}]}'




The response is:
{
  "error": {
    "code": 401,
    "message": "The request does not have valid authentication credentials.",
    "status": "UNAUTHENTICATED"
  }
}


Please advise,
Shushu

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsub...@googlegroups.com.

Kir Titievsky

unread,
Nov 16, 2017, 9:50:01 AM11/16/17
to Dan Cowell, Google Cloud Pub/Sub Discussions
Dan, We rely on generic Oauth2 flows.  The documentation and examples here (and this sub-page in particular) seem like a decent introduction. If I'm wrong, please yell at me. I also find the Oauth2 playground to be good way to understand these flows in detail. 

To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsubscrib...@googlegroups.com.
--

Kir Titievsky | Product Manager | Google Cloud Pub/Sub

--
You received this message because you are subscribed to the Google Groups "Google Cloud Pub/Sub Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-pubsub-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dan Cowell

unread,
Nov 16, 2017, 8:41:33 PM11/16/17
to Kir Titievsky, Google Cloud Pub/Sub Discussions
Hi Kir,

That link looks like exactly what I need, specifically the HTTP/REST part of the Service Accounts section. Not sure how I didn't see it last time I was looking, I must be blind.

Thanks for pointing me in the right direction!

Reply all
Reply to author
Forward
0 new messages