How to generate token for google authentication token?

12/9/2015

I am requesting for authentication/authorization token using curl command like following:

curl -H "Content-Type: application/json" -d'{
"grant_type" : "authorization_code",
"code" : "4/bA5MjGf4emw3hkhCVuTZeJUjughQgJ21l-dCyfpPHdg",
"client_id" : "757054420263-0ajoc014s2dn91b8iko0vj2jtl5pdjfh.apps.googleusercontent.com",
"client_secret" : "xMElPZXcU-ajMUNUwKTksKpV",
"redirect_uri" : "https://www.googleapis.com/auth/cloud-platform"
}' https://accounts.google.com/o/oauth2/token

But it gives me following error:

{
  "error" : "invalid_request",
  "error_description" : "Required parameter is missing: grant_type"
}

And can I use this token for accessing API.

-- Vishwas
google-kubernetes-engine
google-oauth
kubernetes
oauth

1 Answer

12/9/2015

You need to POST the data as form-encoded parameters instead of POSTing them as JSON, so:

curl -d "grant_type=authorization_code" -d "code=4/bA5MjGf4emw3hkhCVuTZeJUjughQgJ21l-dCyfpPHdg" -d "client_id=757054420263-0ajoc014s2dn91b8iko0vj2jtl5pdjfh.apps.googleusercontent.com" -d "client_secret=xMElPZXcU-ajMUNUwKTksKpV" -d "redirect_uri=https://www.googleapis.com/auth/cloud-platform" https://accounts.google.com/o/oauth2/token
-- Hans Z.
Source: StackOverflow