Javascript 通过onClick()使用Google Plus自定义图像按钮登录

Javascript 通过onClick()使用Google Plus自定义图像按钮登录,javascript,onclick,google-plus,Javascript,Onclick,Google Plus,我有一个html文件,上面有“使用google plus登录”按钮 <div class="sub_but"> <a href="javascript:void(0)" onclick="googlepluslogin();" class="googleplus_but" ><i class="fa fa-google-plus"></i> Sign in with Google Plus</a> </div> 当我点

我有一个html文件,上面有“使用google plus登录”按钮

<div class="sub_but"> <a href="javascript:void(0)" onclick="googlepluslogin();" class="googleplus_but" ><i class="fa fa-google-plus"></i> Sign in with Google Plus</a> </div>

当我点击这个时,我需要从谷歌获得“用户名,电子邮件”,我需要通过ajax传递这些数据

为了从谷歌获取数据,我找到了以下代码

代码


(功能(){
var po=document.createElement('script');
po.type='text/javascript';po.async=true;
po.src=https://plus.google.com/js/client:plusone.js';
var s=document.getElementsByTagName('script')[0];
s、 parentNode.insertBefore(po,s);
})();
用户注册
名字
电子邮件地址
密码
注册
var gpclass=(函数(){
//在这里定义类变量
var响应=未定义;
返回{
//类函数/对象
mycodesignin:功能(响应){
//用户已登录
if(响应['access_token']){
//从Google Plus API获取用户信息
gapi.client.load('plus','v1',this.getUserInformation);
}else if(响应['error']){
//出现错误,这意味着用户未登录。
//警报('出现错误:'+authResult['error']);
}
},
getUserInformation:函数(){
var request=gapi.client.plus.people.get({'userId':'me'});
请求.执行(函数(配置文件){
var email=profile['emails'].过滤器(函数(v){
return v.type==“account”;//过滤掉主电子邮件
})[0]。值;
var fName=profile.displayName;
$(“#inputFullname”).val(fName);
$(“#输入电子邮件”).val(电子邮件);
});
}
};//返回结束
})();
功能mycodesignin(gpSignInResponse){
gpclass.mycodesignin(gpSignInResponse);
}
但是我没有意识到在我的
onClick()中使用了它。


提前感谢。

您可以将Google登录流附加到自定义按钮,而不是编写自己的onClick处理程序

auth2 = gapi.auth2.init({
    client_id: '<YOUR_CLIENT_ID>.apps.googleusercontent.com',
    cookiepolicy: 'single_host_origin',
    scope: '<additional_scopes>'
});

element = document.querySelector('.googleplus_but');

auth2.attachClickHandler(element, {},
  function(googleUser) {
    console.log('Signed in: ' + googleUser.getBasicProfile().getName();
  }, function(error) {
    console.log('Sign-in error', error);
  }
);
auth2=gapi.auth2.init({
客户端id:“.apps.googleusercontent.com”,
cookiepolicy:“单主机源”,

范围:'了解详细信息。

感谢您的回复。您能在我的HTML中显示它吗?
auth2 = gapi.auth2.init({
    client_id: '<YOUR_CLIENT_ID>.apps.googleusercontent.com',
    cookiepolicy: 'single_host_origin',
    scope: '<additional_scopes>'
});

element = document.querySelector('.googleplus_but');

auth2.attachClickHandler(element, {},
  function(googleUser) {
    console.log('Signed in: ' + googleUser.getBasicProfile().getName();
  }, function(error) {
    console.log('Sign-in error', error);
  }
);