How to Retrieve Phone Number with Google OAuth API. How can we obtain a phone number using Google OAuth API login?
I'm using the following scope:
'scope': 'https://mail.google.com https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.login'
And the request is as follows:
var request = gapi.client.plus.people.get({'userId': 'me'});
Is there any scope we can use to get it? I get a response but don't see the 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
}
Solution:
If you want to retrieve the user's phone number, you must have authorization from the user. See the following information page: https://developers.google.com/admin-sdk/directory/v1/guides/authorizing.
Request the user to grant this authorization scope: https://www.googleapis.com/auth/admin.directory.user.readonly.
Once you have authorization from the user, run the following request:
GET https://www.googleapis.com/admin/directory/v1/users/userKey
The response will be a JSON formatted response as follows: https://developers.google.com/admin-sdk/directory/v1/reference/users#resource.
One of the attributes will be a list of phone numbers.
Hope this helps.
How to Programmatically Obtain Android Phone Number
How can I programmatically retrieve the phone number of a device running my Android app?
Solution:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Required Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Warning:
According to a highly upvoted comment, there are some warnings to consider. It may return null, an empty string "", or even "???????", and may return an outdated phone number that is no longer valid. If you want something that uniquely identifies the device, you should use getDeviceId() instead.