How can we get the phone number with Google OAuth API and facebook API used for social login?

25,771

Solution 1

if you want to get user phone numbers you have to have authorization from the user: see the following info page : https://developers.google.com/admin-sdk/directory/v1/guides/authorizing

ask the user for this scope of authorization : https://www.googleapis.com/auth/admin.directory.user.readonly

after you have authorization from user run the folowing request : GET https://www.googleapis.com/admin/directory/v1/users/userKey

the response will be a JSON response formatted as followed: https://developers.google.com/admin-sdk/directory/v1/reference/users#resource

one of the attributes is phone list.

hope it helps.

Solution 2

You can use google's people API to get the user's phone numbers. To explore more you can try yourself. Steps to explore:

  1. Visit this link.
  2. Select https://www.googleapis.com/auth/user.phonenumbers.read permission in People API v1 section
  3. Click on Authorize API
  4. Choose the account to log in
  5. Grant permission
  6. Click Exchange authorization code for tokens
  7. Enter https://people.googleapis.com/v1/people/138262720636785143353?personFields=phoneNumbers,emailAddresses link, make sure you replace the UID
  8. Click on send request to see the response
Share:
25,771
Nidhi_s1
Author by

Nidhi_s1

Updated on July 23, 2020

Comments

  • Nidhi_s1
    Nidhi_s1 over 3 years

    How can we get the phone number with Google OAuth API login.

    I am using scopes as

    'scope' : 'https://mail.google.com  https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.login '
    

    and the request is as

    var request = gapi.client.plus.people.get({'userId': 'me'});
    

    Is there any scope we can use to get it.

    I am getting response as with no sight of phone number :

    {
     "kind": "plus#person",
     "etag": "\"vPymIyv1bT9LfmoUujkgN2yLMK0\"",
     "gender": "male",
     "emails": [
      {
       "value": "[email protected]",
       "type": "account"
      }
     ],
     "urls": [
      {
       "value": "http://picasaweb.google.com/XXX",
       "type": "otherProfile",
       "label": "Picasa Web Albums"
      }
     ],
     "objectType": "person",
     "id": "4354354334435465",
     "displayName": "XXXXX YYYY",
     "name": {
      "familyName": "XXX",
      "givenName": "YYYYY"
     },
     "url": "https://plus.google.com/1100335464643327",
     "image": {
      "url": "https://lh3.googleusercontent.com/-fgsdgfgU9-jU/AAAAAAAAAAI/AAAAAAAADkM/fgffdgdkM/photo.jpg?sz=50",
      "isDefault": false
     },
     "isPlusUser": true,
     "language": "en",
     "ageRange": {
      "min": 21
     },
     "circledByCount": 59,
     "verified": false
    }
    
  • Noam
    Noam about 3 years
    When I also asked for the googleapis.com/auth/userinfo.profile permission, I could replace the UID in step 7 with "me" and get a reply, but it didn't include the phone number.
  • SebastianG
    SebastianG over 2 years
    Hmm, I've used this after adding and verifying a phone number for my profile and I'm not getting the phone number data from the Oauth playground api :(
  • Mathews Mathai
    Mathews Mathai about 2 years
    @SebastianG The scope description says Get the phone numbers in user's Google profile. This most likely means the phone numbers linked to or present on Google plus profile and not the Google Account itself. If you look at responses for other scopes like birthdays returns an array of birthdays with source type as Account and Profile respectively. I believe phone numbers don't work that way. Check this Out
  • Mathews Mathai
    Mathews Mathai about 2 years
    @SebastianG I just noticed phone numbers are made available if the user adds them in the Contact Info section under google personal info (About me) settings. myaccount.google.com/profile