Saiban Ali
Saiban Ali

Reputation: 51

Get user's phone number from google people api?

Can I get the phone number of a user using People API? If not is there any other API that I can use to get a user's phone number? I can't seem to find a anything on that.

below code is what I've tried First I signIn using auth2.

useEffect(() => {
    console.log("window: ", window);
    window.gapi.load("client:auth2", initClient);
}, []);

const initClient = () => {
   window.gapi.auth2.init({
      client_id: "CLIENT_ID",
   });
   authenticate().then(loadClient);
};

  const authenticate = () => {
    return window.gapi.auth2
      .getAuthInstance()
      .signIn({
        scope:
          "https://www.googleapis.com/auth/contacts 
           https://www.googleapis.com/auth/contacts.readonly 
           https://www.googleapis.com/auth/directory.readonly 
           https://www.googleapis.com/auth/user.addresses.read 
           https://www.googleapis.com/auth/user.birthday.read 
           https://www.googleapis.com/auth/user.emails.read 
           https://www.googleapis.com/auth/user.gender.read 
           https://www.googleapis.com/auth/user.organization.read 
           https://www.googleapis.com/auth/user.phonenumbers.read 
           https://www.googleapis.com/auth/userinfo.email 
           https://www.googleapis.com/auth/userinfo.profile",
      })
      .then(
        (response) => {
          console.log("sign in successful: ", response);
        },
        (err) => {
          console.log("error signing in: ", err);
        }
      );
  };

Then I load the client using.

  const loadClient = () => {
    window.gapi.client.setApiKey("API_KEY");
    return window.gapi.client
      .load("https://people.googleapis.com/$discovery/rest?version=v1")
      .then(
        () => {
          console.log("GAPI client loaded for api");
        },
        (err) => {
          console.log("error loading GAPI client for api: ", err);
        }
      );
  };

Finally I execute this request to people api to get the info. I can get the name and email address of the logged in user but I am not getting phone number.


  const execute = () => {
    return window.gapi.client.people.people
      .get({
        resourceName: "people/me",
        personFields: "names,emailAddresses,phoneNumbers",
      })
      .then(
        (response) => {
          console.log("people response: ", response);
        },
        (err) => {
          console.log("people err: ", err);
        }
      );
  };

Upvotes: 3

Views: 5479

Answers (2)

E962
E962

Reputation: 726

I believe the reason for this issue is that the phone number is added to the google account as a "contact number". I was having the same issue and found a link to view the phone number here. This is the phone number that you were attempting to fetch and you may find that it is empty because google populates "contact number" when you register, and the "contact number" is the phone number on the main google account page.

I am not entirely sure on how to fetch that "contact number" but that is where I believe the issue is.

Feel free to compare the two different pages here:

https://myaccount.google.com/u/1/profile/phone/edit?index=0&pageId=none (this is the one the api you used is able to access)

https://myaccount.google.com/u/1/phone

Upvotes: 2

Alexandre Cox
Alexandre Cox

Reputation: 518

You will need the user.phonenumbers scope. From the OAuth 2.0 Scopes section:

View your/current user phone numbers

https://www.googleapis.com/auth/user.phonenumbers.read

You can then use people.get("people/me") with personFields="phoneNumbers" to get a Person instance with the phoneNumbers field populated.

Upvotes: 3

Related Questions