How to get OAuth2 token using REST API

Hello,

   I am new to GCP. My goal is to use REST APIs to retrieve my project resources like "list buckets", "list instances" etc... for which I need an OAuth2 token. My attempts to acquire the OAuth2 token using Postman has been failing with this error

{
"error": "unsupported_grant_type",
"error_description": "Invalid grant_type: "
}

Given below is the url I am using for getting OAuth2 Token

https://oauth2.googleapis.com/token

With Headers include:

client_id : <client id from application_default_credentials.json>

client_secret : <client secret from application_default_credentials.json>

refresh_token: <refresh_token from application_default_credentials.json>

type:"authorized_user" 

universe_domain: "googleapis.com"

quota_project_id: <quota_project_id from application_default_credentials.json>

quota_project_id

I am using the values found in application_default_credentials.json created from running "gcloud auth application-default login" command for the values for the header keys (above). Can someone tell me what's wrong ?

1 2 186
2 REPLIES 2

Hello @sivas ,

To get an OAuth2 token using a REST API, you can use the stubby command. The command is:

stubby --proto2 call blade:sso CorpLogin.Exchange << END
target: {
scope: GAIA_USER
name: "${USER}@google.com"
}
target_credential: {
type: OAUTH2_TOKEN
oauth2_attributes: {
scope: 'https://www.googleapis.com/auth/drive.readonly'
}
}
END

The expected output is an oauth2_token.

Hello LeAnt,

  Thanks for your response. But, I am not familiar with stubby. Is there a curl command that can be run using Client_Id, Client_Secret from the Application Default Credentials to obtain the OAuth2 token ?

Thanks!

Top Labels in this Space