Consume RESTful API Endpoints within a golang application with webtoken

78 views
Skip to first unread message

Renato Marcandier

unread,
Mar 11, 2020, 11:13:37 PM3/11/20
to golan...@googlegroups.com

Hello guys,

do you have any HelloWorld project in GO using RESTful to issue HTTP requests from within a Golang application to remote RESTful/API with web token that expires every 60 minutes and then consume any data that is returned?

I am trying to get a simple HelloWorld. First, I need to do a POST to grab the new token and after using this token to do a GET and see the response with "Hello World!" using the following parameters:

-------------------------------------------------------------

POST:


Using:

grant_type: password

client_id: xxxxx

client_secret: xxxxx

username: xxxxx

password: xxxxx


URL: https://cloudsso.cisco.com/as/token.oauth2?grant_type=password&client_id=xxxxxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&username=xxxxxxx&password=xxxxxx


Response:

{

"access_token": "tMqvXkR4wTvFwKUNUvMrNUXdzkjD",

}


-------------------------------------------------------------

GET:


Using: Bearer Token

URL: https://api.cisco.com/hello


Response:

{"helloResponse":{"response":"Hello World!"}}


Regards
RG

Benjamin

unread,
Mar 11, 2020, 11:51:38 PM3/11/20
to Renato Marcandier, golan...@googlegroups.com
Easy, Store the expired time in the token, and hash them.
When the expired time almost reach, use the old token to get a new token.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGbJOOiT8fm7z6t5pNiFSmkXZy2KXs8Xdw-Cq0BkUD1CbUNExA%40mail.gmail.com.

Ingo Oeser

unread,
Mar 15, 2020, 2:20:37 AM3/15/20
to golang-nuts
I can only recommend https://pkg.go.dev/golang.org/x/oauth2/clientcredentials?tab=doc if you are implementing that.

Configure clientcredentials.Config and use the http.Client returned by the Client method of that Config to do all your http calls.

It manages all the token refreshing automatically and transparently for you and is pretty battle hardened already.

Reply all
Reply to author
Forward
0 new messages