Created
September 25, 2018 09:23
-
-
Save eruffaldi/98cfb6cc5b11a3778ac9dcb507ac75f4 to your computer and use it in GitHub Desktop.
Google OAuth 2.0 full example bash script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#1) on https://console.developers.google.com/ register project and associate API from library | |
# OUTPUT: client_id,client_secret | |
client_id="..." | |
client_secret="...." | |
#2) get authorization code at the following link using web browser | |
# OUTPUT: code | |
scope="https://www.googleapis.com/auth/drive" | |
url="https://accounts.google.com/o/oauth2/auth?client_id=$client_id&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=$scope&response_type=code" | |
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" https://accounts.google.com/o/oauth2/token | |
refresh_token="..." | |
token="..." | |
#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" https://accounts.google.com/o/oauth2/token | |
#5) get status of token | |
curl "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$token" | |
#6) use the token in a specific API (assed ass access_token=$token) | |
docid="...." | |
curl "https://docs.google.com/spreadsheets/d/$docid/export?format=csv&access_token=$token" | |
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 :(
in step 2 how to extract code from above url?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the step 3, I could not obtain the refresh_token. I only could obtain the access_token but not the refresh_token