1

我已经从教程https://developers.google.com/+/web/signin/中设置了我的简单 Google 登录按钮,我想知道我是否可以执行以下操作:

  1. 限制电子邮件字段中的所有域,@domain.com 之类的域除外,因此 johndoe@gmail.com 将不起作用,但 johndoe@twitter.com 将被接受。说得通?

  2. 如果用户已经登录 Google 帐户,是否可以“强制”用户再次登录?

这是我的 Javascript / jQuery 代码:

<script type="text/javascript">

      (function() {
       var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
       po.src = 'https://apis.google.com/js/client:plusone.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
     })();


  function signinCallback(authResult) {
  if (authResult['access_token']) {
    // Successfully authorized
    // Hide the sign-in button now that the user is authorized, for example:
    document.getElementById('signinButton').setAttribute('style', 'display: none');

    $('.inline-field-title').hide();


  } else if (authResult['error']) {
    // There was an error.
    // Possible error codes:
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatically log in the user
    // console.log('There was an error: ' + authResult['error']);
  }
}


  function disconnectUser(access_token) {
  var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
      access_token;

  // Perform an asynchronous GET request.
  $.ajax({
    type: 'GET',
    url: revokeUrl,
    async: false,
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(nullResponse) {
      // Do something now that user is disconnected
      // The response is always undefined.
    },
    error: function(e) {
      // Handle the error
      // console.log(e);
      // You could point users to manually disconnect if unsuccessful
      // https://plus.google.com/apps
    }
  });

}
// Could trigger the disconnect on a button click

$('#revokeButton').click(disconnectUser);


</script>
4

1 回答 1

4

广告 1) 您必须在登录后手动检查电子邮件,如果电子邮件与您要查找的任何域不匹配,请采取相应措施。

为此,您必须请求一个额外的范围https://www.googleapis.com/auth/userinfo.email,您可以在“data-scope登录”按钮标记的参数中定义该范围,并在身份验证后执行经过身份验证的请求,https://www.googleapis.com/oauth2/v2/userinfo该请求将在响应中返回电子邮件地址。

有关一些示例代码,请参阅https://developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address

广告 2) 登录按钮接受一个参数,该参数data-approvalprompt="force"将在每次登录尝试时显示身份验证对话框(并让用户切换到不同的帐户)。

请参阅https://developers.google.com/+/web/signin/#sign-in_button_attributes

于 2013-09-25T00:46:16.167 回答