Reputation: 43
We created a google sheet add on, and would like to communicate with people who installed it.
We know how to get the user's email after he opens the sheet for the first time, but we're trying to get the email when he accepts the scopes.
Is it possible ? i can't find any related doc on the google sites.
Thanks !
Upvotes: 0
Views: 135
Reputation: 3725
First you need to make sure that your application is requesting the https://www.googleapis.com/auth/userinfo.email
scope.
Then, when authenticating with OAuth and requesting the access token the response will also include an id_token
field. The response will look something like this:
{
"access_token": <the access token>,
"id_token": <a long id token>,
"expires_in": 3599,
"token_type": "Bearer",
"scope": "openid https://www.googleapis.com/auth/userinfo.email <other scopes>",
"refresh_token": <the refresh token>
}
Afterwards you can use the Google endpoint https://oauth2.googleapis.com/tokeninfo?id_token=<your id token>
to decode the id_token. It will return a JSON object like this:
{
"iss": "https://accounts.google.com", // The JWT's issuer
"nbf": 161803398874,
"aud": "314159265-pi.apps.googleusercontent.com", // Your server's client ID
"sub": "3141592653589793238", // The unique ID of the user's Google Account
"hd": "gmail.com", // If present, the host domain of the user's GSuite email address
"email": "[email protected]", // The user's email address
"email_verified": true, // true, if Google has verified the email address
"azp": "314159265-pi.apps.googleusercontent.com",
"name": "Elisa Beckett",
// If present, a URL to user's profile picture
"picture": "https://lh3.googleusercontent.com/a-/e2718281828459045235360uler",
"given_name": "Elisa",
"family_name": "Beckett",
"iat": 1596474000, // Unix timestamp of the assertion's creation time
"exp": 1596477600, // Unix timestamp of the assertion's expiration time
"jti": "abc161803398874def"
}
From here you can grab the email field for your own purposes.
The id_token
is a JSON Web Token (JWT) string so you can also use other tools to decode it, not just the Google endpoint.
You can test this yourself with the scope above in Google's OAuth Playground too.
Reference: https://developers.google.com/identity/gsi/web/reference/js-reference#credential
Upvotes: 1