2
votes

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);
        }
      );
  };
1
Are you using a specific programming language, or the REST api? What have you tried so far?Alexandre Cox
I am building a website with react. I 've tried using People API but wasn't able to get user's phone number.Saiban Ali
Maybe add a code sample where you use the api and expect the contact/person info. Showing the code where you build the people service, as well as the scopes used, would also be helpful.Alexandre Cox
Also, don't forget to read the People API doc (developers.google.com/people) and perhaps the JavaScript client library for the people API, if that is what you are using (github.com/google/google-api-javascript-client/blob/master/docs/…)Alexandre Cox
I have edited my question with what I have done to get the phone number. I can get the name and email address using people/me end point but not getting phone number. I have added phone numbers read scope. On the docs it is listed 'phoneNumbers' as a possible value for personFields but I don't why it's not working...Saiban Ali

1 Answers

2
votes

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.