google 登录 auth2 自定义范围,无需 openid

google sign-in auth2 customize scope without openid

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

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

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

或 js:

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

但它不起作用:在生成的 URL 中始终存在 "openid" 范围...

我怎样才能 "reset" 范围,并且只允许我想要的?

BasicProfile 需要 'profile email openid' 个范围。

您可以禁用基本配置文件,只需要 'profile email' 个范围:

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

请注意,在这种情况下,GoogleUser.getBasicProfile() 将 return 为空。您必须手动获取 'profile' 和 'email' 数据。

完整示例(您必须将 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 
        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 才能请求 'profile' 数据。为此

  1. 转到https://console.developers.google.com/
  2. select 项目
  3. 转到 APIs & auth > APIs
  4. 找到并启用 Google+ API.