登录使用Google Plus自定义图像按钮与onClick()
Signin using Google Plus custom image button with onClick()
我有一个html文件'signin with 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传递这个数据。
从谷歌获取数据,我找到了这个代码developers.google
代码<script type="text/javascript">
(function() {
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);
})();
</script>
</head>
<body>
<div class="container">
<form class="form-signin" role="form">
<div id="status"></div>
<h2 class="form-signin-heading">User Registration</h2>
<label for="inputFname" class="sr-only">First Name</label>
<input type="text" id="inputFullname" class="form-control" placeholder="First Name" required autofocus>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required >
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="row">
<div class="col-md-6">
<button class="btn btn-sm btn-primary btn-block" type="submit">Sign Up</button>
</div>
<div class="col-md-6">
<button class="g-signin "
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-clientId="MY-ID-GIVEN"
data-accesstype="offline"
data-callback="mycoddeSignIn"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>
</div>
</div>
</form>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
var gpclass = (function(){
//Defining Class Variables here
var response = undefined;
return {
//Class functions / Objects
mycoddeSignIn:function(response){
// The user is signed in
if (response['access_token']) {
//Get User Info from Google Plus API
gapi.client.load('plus','v1',this.getUserInformation);
} else if (response['error']) {
// There was an error, which means the user is not signed in.
//alert('There was an error: ' + authResult['error']);
}
},
getUserInformation: function(){
var request = gapi.client.plus.people.get( {'userId' : 'me'} );
request.execute( function(profile) {
var email = profile['emails'].filter(function(v) {
return v.type === 'account'; // Filter out the primary email
})[0].value;
var fName = profile.displayName;
$("#inputFullname").val(fName);
$("#inputEmail").val(email);
});
}
}; //End of Return
})();
function mycoddeSignIn(gpSignInResponse){
gpclass.mycoddeSignIn(gpSignInResponse);
}
</script>
但我不知道在我的onClick().
您可以将Google Sign-in流程附加到您的自定义按钮上,而不是编写自己的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);
}
);
详情请参阅文档中的相关部分
相关文章:
- 如何将按钮(onclick事件)与jQuery自动完成连接
- 使用按钮OnClick事件调用Javascript函数
- 如果没有历史记录,我如何隐藏图像或按钮onclick=;历史.go(-1);
- 按钮onclick函数在jquery中无法正常工作
- 按钮onclick应该与参数一起重定向到另一个xhtml页面
- JavaScript将参数传递给按钮onclick函数
- ASP.net 链接按钮“onclick”如果用作UpdatePanel中的AsyncPostBackTrigger触发
- 按钮 Onclick 函数与 document.getElementById.
- 使用单选按钮OnClick和JavaScript功能动态填充数字字段
- 将 PHP 中的值传递到 PHP while 循环使用 按钮 OnClick 到 JavaScript
- Javascript 按钮 OnClick 找不到我想要的方法
- 单选按钮onclick事件在Chrome上不起作用
- 将按钮onclick事件绑定到Backbone View
- 如何重置/取消选中单选按钮onclick事件
- 页面加载时触发按钮onclick
- 在按钮onclick&将数据保存到数据库
- 用符合内容安全策略的代码替换多个内联按钮onclick事件处理程序
- 使用Cordova Webview/Phonegap按钮onclick(eventlistener)更改本机视图
- 使用按钮onclick时不显示警报
- 在按钮OnClick中运行php函数(不提交)