Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get user info profile from Google API?

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:

  • Get code in url redirect_uri by client_id
  • Get token https://accounts.google.com/o/oauth2/token by code, client_id, client_secret ...
  • Try call to https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=accessToken to get info but only object
{
    "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

like image 656
Vũ Anh Dũng Avatar asked Apr 01 '19 06:04

Vũ Anh Dũng


People also ask

What data can I get from Google login?

After you have signed in a user with Google using the default scopes, you can access the user's Google ID, name, profile URL, and email address.

What is userId in Gmail API?

userId. string. The user's email address. The special value me can be used to indicate the authenticated user.


1 Answers

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'];
like image 93
DaImTo Avatar answered Sep 30 '22 09:09

DaImTo