获取用户信息google plus api [英] getting user info google plus api

查看:111
本文介绍了获取用户信息google plus api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get public info of a user from google plus login button integrated on the site, here is the code which is giving me email, I need more info which is provide by 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');
    }
  }

我尝试用名称userId替换电子邮件,但是从这些变量中什么也没得到.

I tried replacing email with name, userId but getting nothing from these variables.

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

How can I get basic information of a user when he is logged in through google plus.

推荐答案

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

Similar to how you have loaded the oauth2 v2 client package using gapi.client.load, you will use this again to load the plus v1 client package. This will give you a number of packages and methods under the gapi.client.plus namespace.

Plus API包含一个程序包,用于加载有关人员的信息,包括通过其用户ID获取人员信息,或者由于已通过您的身份验证,因此您可以使用特殊标识符"me".

The Plus API includes a package to load information about People, including getting them by their User ID or, since they have authenticated with you, you can use the special identifier "me".

有关详细信息和示例,请参见 https://developers.google. com/+/api/latest/people/get ,但这是与getEmail()方法类似的(未经测试的)函数,该函数将获得其全名:

Full details and an example are given at https://developers.google.com/+/api/latest/people/get, but here is an (untested) similar function to your getEmail() method that would get their full name:


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');
};

这篇关于获取用户信息google plus api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆