登录我的网站使用google api

时间:2014-09-10 07:12:10

标签: javascript google-api google-plus google-api-client google-authentication

我正在尝试使用google登录我的网站。我把代码放在jsp页面的html代码中:

<span id="signinButton">
 <span
class="g-signin"
data-callback="signinCallback"
data-clientid="*********"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schema.org/AddAction"
data-scope="https://www.googleapis.com/auth/plus.login">
 </span>
 </span>

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['status']['signed_in']) {
        // Update the app to reflect a signed in user
        // Hide the sign-in button now that the user is authorized, for example:
        document.getElementById('signinButton').setAttribute('style', 'display: none');
        var request = gapi.client.plus.people.get({
              'userId' : 'me'
            });
        request.execute(function(resp) {
              console.log('ID: ' + resp.id);
              console.log('Display Name: ' + resp.displayName);
              console.log('Image URL: ' + resp.image.url);
              console.log('Profile URL: ' + resp.url);
            });
      } else {
        // Update the app to reflect a signed out user
        // Possible error values:
        //   "user_signed_out" - User is signed-out
        //   "access_denied" - User denied access to your app
        //   "immediate_failed" - Could not automatically log in the user
        console.log('Sign-in state: ' + authResult['error']);
      }
    }

如果我把数据范围地址http设为错误:

  1. 这是一个错误。
  2. 错误:invalid_request

    范围http://www.googleapis.com/auth/plus.login

    不允许使用request_visible_actions
    1. 这是一个错误。
    2. 错误:origin_mismatch

      申请:项目默认服务帐户

      如何解决这个问题。我希望用户个人资料详细信息使用google api?

1 个答案:

答案 0 :(得分:1)

删除data-requestvisibleactions="http://schema.org/AddAction",确保在您允许的来源的应用程序列表中注册了执行代码的确切URL和路径。将范围更改为data-scope="profile"