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);
}
);
};