3

我想自定义范围以仅允许“电子邮件”和“个人资料”,而没有“openid”
,因为我想让它只要求访问电子邮件和基本个人资料信息。

我尝试使用元数据来做到这一点:

<meta name="google-signin-scope" content="email profile">

或js:

gapi.auth2.init({
    client_id: 'xxxxxxxxx.apps.googleusercontent.com',
    scope: 'email profile'
});

但它不起作用:在生成的 URL 中总是有“openid”范围......

我怎样才能“重置”范围,只允许我想要的?

4

1 回答 1

4

BasicProfile 需要“个人资料电子邮件 openid”范围。

您可以禁用基本配置文件并只需要“配置文件电子邮件”范围:

<meta name="google-signin-fetch_basic_profile" content="false">
<meta name="google-signin-scope" content="profile email">

请注意,在这种情况下,GoogleUser.getBasicProfile() 将返回 null。您必须手动获取“个人资料”和“电子邮件”数据。

完整的工作示例(您必须将 YOUR_CLIENT_ID 替换为您的实际 client_id):

<html>
<head>
   <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
   <meta name="google-signin-fetch_basic_profile" content="false">
   <meta name="google-signin-scope" content="profile email">
</head>
<body>
  <script>
    function requestEmailData() { 
      gapi.client.load('oauth2', 'v2', function() {
        gapi.client.oauth2.userinfo.get().execute(function(resp) {
          // Shows user email
          console.log(resp.email);
        })
      });
    }

    function requestProfileData() {
      gapi.client.load('plus', 'v1', function() {
        gapi.client.plus.people.get( {'userId' : 'me'} ).execute(function(resp) {
          // Shows profile information
          console.log(resp);
        })
      });
    }

    function onSuccess() {
      gapi.load('client', function() {
        // based on http://stackoverflow.com/a/15384981
        requestEmailData();
        requestProfileData();
      });
    }
  </script>

  <div class="g-signin2" data-onsuccess="onSuccess"></div>

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

请注意,您必须在开发者控制台中启用 Google+ API 才能请求“个人资料”数据。要做到这一点

  1. 转到https://console.developers.google.com/
  2. 选择项目
  3. 去 APIs & auth > APIs
  4. 查找并启用 Google+ API。
于 2015-04-27T18:24:40.213 回答