0

如何从网站上集成的 google plus 登录按钮获取用户的公共信息,这是给我电子邮件的代码,我需要 google plus 提供的更多信息:

  <div id="signin-button" class="show">
     <div class="g-signin" data-callback="loginFinishedCallback"
                        data-approvalprompt="force"
                        data-clientid="9076269517.apps.googleusercontent.com"
                        data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
                        data-height="short"
                        data-cookiepolicy="single_host_origin">
</div>

java脚本:

  function loginFinishedCallback(authResult) {
    if (authResult) {
      if (authResult['error'] == undefined){
        gapi.auth.setToken(authResult); // Store the returned token.
        toggleElement('signin-button'); // Hide the sign-in button after successfully signing in the user.
        getEmail();                     // Trigger request to get the email address.
      } else {
        console.log('An error occurred');
      }
    } else {
      console.log('Empty authResult');  // Something went wrong
    }
  }

function getEmail(){
    // Load the oauth2 libraries to enable the userinfo methods.
    gapi.client.load('oauth2', 'v2', function() {
          var request = gapi.client.oauth2.userinfo.get();
          request.execute(getEmailCallback);
        });
  }

  function getEmailCallback(obj){
    var el = document.getElementById('email');
    var email = '';

if (obj['email']) {
  email = 'Email: ' + obj['email'];
}

//console.log(obj);   // Uncomment to inspect the full object.

el.innerHTML = email;
toggleElement('email');
  }



 function toggleElement(id) {
    var el = document.getElementById(id);
    if (el.getAttribute('class') == 'hide') {
      el.setAttribute('class', 'show');
    } else {
      el.setAttribute('class', 'hide');
    }
  }

我尝试用名称、用户 ID 替换电子邮件,但从这些变量中一无所获。

通过google plus登录时如何获取用户的基本信息。

4

2 回答 2

2

与您使用 加载 oauth2 v2 客户端包的方式类似gapi.client.load,您将再次使用它来加载 plus v1 客户端包。这将为您提供命名空间下的许多包和方法gapi.client.plus

Plus API 包含一个包来加载有关人员的信息,包括通过他们的用户 ID 获取他们,或者由于他们已经通过您的身份验证,您可以使用特殊标识符“我”。

https://developers.google.com/+/api/latest/people/get提供了完整的详细信息和示例,但这里有一个(未经测试的)与您的 getEmail() 方法类似的函数,该函数将获得其全名:


function getFullName(){
  // Load the Plus library to get the People package and methods
  gapi.client.load('plus', 'v1', function() {
    var request = gapi.client.plus.people.get('me');
    request.execute(getFullNameCallback);
  });
};

function getFullNameCallback(obj){
  var el = document.getElementById('email');
  var name = '';

  if (obj['displayName']) {
    name = 'Name: '+obj.displayName;
  }

  el.innerHTML = name;
  toggleElement('name');
};
于 2013-07-11T12:59:20.877 回答
0

上面的代码片段似乎不再起作用。我们又一次追赶谷歌现在改变的东西......错误“访问未配置。请使用谷歌开发者控制台为您的项目激活 API。”

我以为它可能是“Google+ API”,所以它在开发者控制台中打开了,但是仍然没有工作。

然而api explorer有希望地表明某种代码可以工作,但它是一个狗早餐,试图辨别哪些 javascript 代码在那里工作。如此有用的 api explorer....,但是谷歌如何在代码中显示一个简单的 WORKING 示例,我们可以查看这个相同的请求?

于 2014-02-19T06:23:01.910 回答