How to get Google access token programmatically (automated testing)?
Create a project on Google-console: https://console.cloud.google.com/
Go to Credentials -> + CREATE CREDENTIALS -> OAuth client ID:
- Application Type: Web
- Name: Some application's name
- Authorized redirect URIs: https://developers.google.com/oauthplayground + (optionally) your real callback handlers, e.g. https://example.com/auth/google/callback.
- Press Create.
- Save Client ID and Client Secret.
Press top right settings (gear) icon (OAuth 2.0 configuration):
- (!) Force prompt: Consent Screen
- Tick "Use your own OAuth credentials" and enter OAuth Client ID and OAuth Client secret, close the OAuth 2.0 configuration" screen.
At the bottom of the Step 1 accordion panel enter required space-separated scopes (see https://developers.google.com/identity/protocols/oauth2/scopes#oauth2):
openid (default)
Example:
openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
and press Authorize APIs.
On the next screen choose the account (optional screen) and give the permissions to the app.
Then you should be redirected to https://developers.google.com/oauthplayground/?code=SOME-SINGLE-USE-CODE&scope=openid&...
Press Exchange authorization code for tokens
OR manually via
curl -v "https://oauth2.googleapis.com/token" -d "code=SOME-SINGLE-USE-CODE&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=YOUR_APP_CLIENT_ID&client_secret=YOUR_APP_CLIENT_SECRET&scope=&grant_type=authorization_code"
Get your non-expiring refresh token.
Finally, get access token from Access token
OR manually:
curl -d "client_id=YOUR_APP_CLIENT_ID&client_secret=YOUR_APP_CLIENT_SECRET&grant_type=refresh_token&refresh_token=YOUR_APP_REFRESH_TOKEN" "https://oauth2.googleapis.com/token"
Validate:
curl "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=ACCESS_TOKEN"
{
"id": "10934363016XXXXXXXXXX",
"picture": "https://lh3.googleusercontent.com/a-/AOh14GhoUlKjYgC-..."
}
Finally:
Enable API
https://console.cloud.google.com/apis/library/adsense.googleapis.com?project=webapp-{number}
or
https://console.cloud.google.com/apis/library/adsense.googleapis.com?project={client_id}
Комментариев нет:
Отправить комментарий