11

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": "XXX@gmail.com",
   "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
}
Nidhi_s1
  • 131
  • 1
  • 1
  • 4

2 Answers2

10

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.

ddor254
  • 1,570
  • 1
  • 12
  • 28
6

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
Imran Ahmad
  • 2,798
  • 3
  • 28
  • 49
  • When I also asked for the https://www.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. – Noam Nov 03 '20 at 21:55
  • 2
    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 :( – SebastianG Mar 05 '21 at 16:16
  • @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](https://stackoverflow.com/questions/46661815/cant-read-phone-numbers-from-users-google-profile-using-auth-user-phonenumbe?noredirect=1&lq=1) – Mathews Mathai Sep 12 '21 at 17:56
  • 4
    @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. https://myaccount.google.com/profile – Mathews Mathai Sep 12 '21 at 18:35