Google JavaScript客户端API:如何获取个人资料名称?

问题描述

我正在尝试通过重定向方法与Google实施SignIn。我正在关注this链接 我的代码如下所示

<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>

但是问题是,即使我通过了fetch_basic_profile: true,我也尝试过scope: 'profile',但Google的id_token却没有名称

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

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

解决方法

不能保证Google ID令牌会在收到响应后返回所有个人资料声明。

如果您想要用户的个人资料信息,则应使用Google People API。 people.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上找到的尝试中撕掉了代码

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...