Google API - OAuth 2.0 - trying to get AccessToken using curl

1.7k Views Asked by At

Trying to get AccessToken using curl but getting error as "https://accounts.google.com/o/oauth2/token: No such file or directory" Can someone help what i am doing wrong?

Mainly, am wondering if I am setting up the client secret correctly, though I'm quite unsure, would really appreciate some guidance.

(have redacted some part by *)

curl \
—request POST \
--data ”code=4/***********************3Uzp--0jT4uNN******rSWSRkHw8&client_id=***************************.apps.googleusercontent.com&client_secret=**********&redirect_uri=https://localhost&grant_type=authorization_code” \https://accounts.google.com/o/oauth2/token
1

There are 1 best solutions below

6
On BEST ANSWER

This is the code i use.

# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space seprated list of the scopes of access you are requesting.

# Authorization link.  Place this in a browser and copy the code that is returned after you accept the scopes.
https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code

# Exchange Authorization code for an access token and a refresh token.

curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token

# Exchange a refresh token for a new access token.
curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token

Code ripped from one of my gists GoogleAuthenticationCurl.sh

update

after the removal of OOB urn:ietf:wg:oauth:2.0:oob should be changed to http://127.0.0.1 look in the url bar the code will be there but you will see a 404 error on the page itself.