【问题标题】:Get user's phone number from google people api?从 google people api 获取用户的电话号码?
【发布时间】:2020-09-23 01:04:25
【问题描述】:

我可以使用 People API 获取用户的电话号码吗?如果没有,我可以使用任何其他 API 来获取用户的电话号码吗?我似乎找不到任何东西。

下面的代码是我尝试过的 首先我使用 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);
        }
      );
  };

然后我使用加载客户端。

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

最后,我向 people api 执行此请求以获取信息。 我可以得到登录用户的姓名和电子邮件地址,但我没有得到电话号码。


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

【问题讨论】:

  • 您使用的是特定的编程语言还是 REST api?到目前为止,您尝试过什么?
  • 我正在建立一个带有反应的网站。我尝试过使用 People API,但无法获取用户的电话号码。
  • 也许在您使用 api 的地方添加一个代码示例并期望联系人/人员信息。显示构建人员服务的代码以及使用的范围也会很有帮助。
  • 另外,别忘了阅读 People API 文档 (developers.google.com/people) 以及可能是用于 people API 的 JavaScript 客户端库,如果您正在使用的话 (github.com/google/google-api-javascript-client/blob/master/docs/…)跨度>
  • 我已经用我为获取电话号码所做的工作编辑了我的问题。我可以使用 people/me 端点获取姓名和电子邮件地址,但无法获取电话号码。我添加了电话号码读取范围。在文档中,它被列为“phoneNumbers”作为 personFields 的可能值,但我不知道为什么它不起作用......

标签: google-people-api


【解决方案1】:

您将需要 user.phonenumbers 范围。来自OAuth 2.0 Scopes section

查看您/当前用户的电话号码

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

然后,您可以使用 people.get("people/me")personFields="phoneNumbers" 来获取填充了 phoneNumbers 字段的 Person 实例。

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多