0

我正在尝试使用重定向方法使用 Google 实现登录。我正在关注链接 我的代码如下所示

<script src="https://apis.google.com/js/platform.js?onload=startGoogleApp" async defer></script>
<script>

    var startGoogleApp = function () {
        gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
                client_id: '@googleClientId',
                ux_mode: 'redirect',
                redirect_uri: '@googleRedirectUri',
                fetch_basic_profile: true
            });

            auth2.signIn();
        });
    }
</script>

但问题在于 Google 的 id_token 没有名称,即使我已经通过了fetch_basic_profile: true,我也尝试过使用scope: 'profile'.

我想获取姓名和电子邮件。不知道我在这里做错了什么。我想要它作为令牌的一部分,因为它在我正在关注的文档中提到。我不想通过额外的 api 调用来获取名称。可能吗?id_token 看起来像这样

{
  "iss": "accounts.google.com",
  "azp": "*********",
  "aud": "***********",
  "sub": "*********",
  "hd": "***.com",
  "email": "*****@***.com",
  "email_verified": true,
  "iat": 1599717107,
  "exp": 1599720707,
  "jti": "*******"
}
4

1 回答 1

0

Google 的 ID 令牌不保证会在任何响应时返回所有配置文件声明。

如果您想要用户个人资料信息,那么您应该通过 Google People API。 人.get

 // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.people.people.get({
      "resourceName": "people/me",
      "requestMask.includeField": "addresses",
      "sources": [
        "READ_SOURCE_TYPE_PROFILE"
      ]
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  });

从我在people.get上找到的试用版中提取的代码

于 2020-09-10T06:10:41.057 回答