7

I have to implement signIn by google account.

I want to some suggestions.

I created project in google console. Added scope user info.profile

I'm following course instruction on internet, but I still cannot get userinfo ( email, name, age ... ).

Step:

{
    "azp": "155122683461-51hq2n932svo4ajbt98ic0q67m4tuj5o.apps.googleusercontent.com",
    "aud": "155122683461-51hq2n932svo4ajbt98ic0q67m4tuj5o.apps.googleusercontent.com",
    "sub": "108865940357700877124",
    "scope": "https://www.googleapis.com/auth/userinfo.profile",
    "exp": "1554094721",
    "expires_in": "3326",
    "access_type": "offline"
}

Can you guys give me an example :(

Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Vũ Anh Dũng
  • 980
  • 3
  • 13
  • 32
  • 1
    Although I'm not sure whether this is the result you want, for example, how about this endpoint? ``GET https://www.googleapis.com/oauth2/v3/userinfo?access_token=accessToken`` – Tanaike Apr 01 '19 at 06:43
  • Sorry. From now I know why api get token ( https://accounts.google.com/o/oauth2/token ) always return error : { "error": "invalid_grant", "error_description": "Bad Request" } – Vũ Anh Dũng Apr 01 '19 at 06:52
  • @VũAnhDũng invalid_grant is a different question i suggest you open a new question with that and add your code. – Linda Lawton - DaImTo Apr 01 '19 at 06:55
  • @DaImTo I tried with ```GET https://www.googleapis.com/oauth2/v3/userinfo?access_token=accessToken``` But response not contain email ??? – Vũ Anh Dũng Apr 01 '19 at 09:16
  • The response will only contain an email if you request the email scope. As i mentioned in my answer. – Linda Lawton - DaImTo Apr 01 '19 at 09:17
  • In config google project (console.develop.google.com) I have added user info.profile to scope. – Vũ Anh Dũng Apr 01 '19 at 09:19
  • ```https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile&response_type=code&access_type=offline&redirect_uri=http://localhost:3001&client_id=607602703319-5654m0luqaaqjq14vjjei27qo86a9ct0.apps.googleusercontent.com``` I also add to url to get Authentication – Vũ Anh Dũng Apr 01 '19 at 09:21
  • Ok thanks. I resolved the issue – Vũ Anh Dũng Apr 01 '19 at 15:10
  • Does this answer your question? [Google OAuth API to get user's email address?](https://stackoverflow.com/questions/24442668/google-oauth-api-to-get-users-email-address) – Eugen Konkov Jul 10 '20 at 17:12

1 Answers1

11

people api

The infomration you are looking for can be found on people.get

GET https://people.googleapis.com/v1/{resourceName=people/*}

tip send Field mask with no space - person.emailAddresses,person.birthdays It reads form person info so the user will have had to fill in this information

However you will need to add the scopes to get the information you want

https://www.googleapis.com/auth/profile.emails.read
https://www.googleapis.com/auth/user.birthday.read

You can test it here Google Apis explorer

A node.js quick start for google people api can be found here

userinfo endpoint

The userinfo endpoint can also be used but it does not return the information you are looking for

You need to request the email scope to have seen email in the below response the user must grant you permission to see their email the following is standard response for profile scope only.

GET /oauth2/v2/userinfo HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: Bearer ya29.GlveBiwp4-NTPLU9VN3rn1enty11KOdQHGcyfZd1xJ1Ee9eGS2Pw2nJ7KDUBQPa-uT-AoKDQdoVigU6bruVIB1a3fiBu1n

response

{
  "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
  "name": "Linda Lawton", 
  "family_name": "Lawton", 
  "locale": "en", 
  "gender": "female", 
  "link": "https://plus.google.com/+LindaLawton", 
  "given_name": "Linda", 
  "id": "117200475532672775346"
}

scopes

You should consult the node tutorial for how to work with scopes. Remember you will need to request access of the user again if you change the scope in your code.

const SCOPES = ['profile', 'email'];
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I can't see that scope in list scope can choice – Vũ Anh Dũng Apr 01 '19 at 06:56
  • which scope and which list? Scopes are just strings you can add what ever you want. – Linda Lawton - DaImTo Apr 01 '19 at 06:56
  • ```GET /oauth2/v2/userinfo HTTP/1.1 Host: www.googleapis.com``` Working but I need user.email in response – Vũ Anh Dũng Apr 01 '19 at 09:20
  • As i mentioned in my answer and in your other comment. You need to add the email scope when authorizing your user if you want return email in userinfo endpoint. – Linda Lawton - DaImTo Apr 01 '19 at 09:26
  • ```https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile,https://www.googleapis.com/auth/profile.emails.read,https://www.googleapis.com/auth/user.birthday.read&response_type=code&access_type=offline&redirect_uri=http://localhost:3001/oauth/google&client_id=607602703319-5654m0luqaaqjq14vjjei27qo86a9ct0.apps.googleusercontent.com``` – Vũ Anh Dũng Apr 01 '19 at 09:34
  • Do you mean set scope to url request authorization? I set it as above but gt error: ```400. That’s an error. Error: invalid_scope Some requested scopes were invalid. {invalid=[https://www.googleapis.com/auth/userinfo.profile,https://ww``` – Vũ Anh Dũng Apr 01 '19 at 09:36
  • check the scope section of my answer. I think that you should open a new question and post your code this is beyond the scope of your current question which was just about which api to use. if you post a new question with your code i would be able to help you address any issues with your code more efficiently – Linda Lawton - DaImTo Apr 01 '19 at 09:38
  • 1
    Thanks. I tried add scope userinfo.email and it working – Vũ Anh Dũng Apr 01 '19 at 15:11
  • Hi, thanks. I have the userinfo call working fine: `"https://www.googleapis.com/oauth2/v1/userinfo` but was just wondering if there are proper GoogleClient libraries (java) somewhere encapsulating this. I don't want to use people API as is not currently enabled for our app and all we want is the email address to display with the token. Thanks – Sodved May 04 '21 at 06:23
  • @Sodved it is not recommend using the userinfo endpoint as its not guaranteed to always return all of the claims, on occasion they will not be returned to you as its not intended to be a profile data resource. If you want to get user profile data then you should use the people api which will give you access to all the data you need. – Linda Lawton - DaImTo May 04 '21 at 07:39
  • All I want is the email that was used to get the refresh token. But I'll talk with our GCP team about enabling the People API – Sodved May 05 '21 at 05:32