Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Google OAuth 2.0 full example bash script.
#1) on register project and associate API from library
# OUTPUT: client_id,client_secret
#2) get authorization code at the following link using web browser
# OUTPUT: code
#3) use the code to obtain a OAuth2.0 Token
# OUTPUT: token and refresh_token in JSON
curl --request POST --data "code=$code&client_id=$client_id&client_secret=$client_secret&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"
#4) refresh if needed
curl --request POST --data "--data 'client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token"
#5) get status of token
curl "$token"
#6) use the token in a specific API (assed ass access_token=$token)
curl "$docid/export?format=csv&access_token=$token"
Copy link

vcollado commented Nov 10, 2018

At line 21,

curl --request POST --data "--data 'client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token"

It must be without the extra --data

curl --request POST --data "client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token""

Copy link

ToddAndMargo commented Mar 19, 2020

Question: Line 27 docid="....", where does this value come from?

Request: would you post an example of sending a cURL smtp message with OAuth2 though gmail?

Copy link

jmcarne commented Jul 6, 2021

In the step 3, I could not obtain the refresh_token. I only could obtain the access_token but not the refresh_token

Copy link

sarnobat commented Aug 10, 2021

Very helpful, thanks for sharing. Now I know why there aren't many code samples that use shell scripting instead of Python etc. Oauth is a lot of work and doesn't really lend itself to linear Unix pipelines :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment