我在项目[内置CodeIgniter]中使用了Google Plus按钮。这里我添加了以下代码。
<span id="signinButton">
<span
class="g-signin gooConnect"
data-callback="signinCallback"
data-clientid="my_project_client_id"
data-cookiepolicy="single_host_origin"
data-requestvisibleactions="http://schemas.google.com/AddActivity"
data-scope="https://www.googleapis.com/auth/userinfo.email">
</span>
</span>
然后我添加了Google提供的Javascript代码。
<script type="text/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['access_token']) {
$.ajax({
url:base_url+'index.php/user/getUserProfile',
type:'POST',
data:{'access':authResult['access_token']},
beforeSend : function(){
$("#loadingImageBeforeResult").show('slow');
},
success : function(resp){
$("#loadingImageBeforeResult").hide('slow');
if( resp == 'exist' ){
window.location.href=base_url+'index.php/user/my_deals';
} else {
$('#link_for_geniepage').trigger('click');
}
},
error : function(resp){}
});
} else if (authResult['error']) {
// There was an error.
// Possible error codes:
// "access_denied" - User denied access to your app
// "immediate_failed" - Could not automatially log in the user
// console.log('There was an error: ' + authResult['error']);
}
}
</script>
它对我来说很好用,但如果我在单独的标签中登录我的Gmail帐户然后转到我的登录页面,则回拨功能只需使用我的Gmail凭据自动登录并将我重定向到我的信息中心。
我希望除非我点击Google Plus按钮,否则回调功能不起作用。我怎样才能做到这一点?请帮帮我。
答案 0 :(得分:3)
在signinCallback(authResult)
函数中,您应首先检查用户是否已登录,然后您应检查方法值是AUTO
还是PROMPT
。 PROMPT
正是您想要的,因为当用户点击登录按钮时会返回它。这是代码:
function signinCallback(authResult) {
if (authResult['status']['signed_in'] && authResult['status']['method'] == 'PROMPT') {
// User clicked on the sign in button. Do your staff here.
} else if (authResult['status']['signed_in']) {
// This is called when user is signed in to Google but hasn't clicked on the button.
} 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']);
}
答案 1 :(得分:2)
从文档中,看起来像登录按钮,当以这种方式使用时,将始终尝试立即验证。由于您已经登录Google并授权该应用,因此Google会自动为您登录并将您转到信息中心。
我建议不要使用该示例代码。相反,您可以使用Google Javascript API的其他部分(https://developers.google.com/+/web/api/javascript)制作一个Google登录按钮,这是一个普通的按钮。单击它后,调用gapi.auth.authorize()
将用户登录。然后在他们单击按钮之前没有任何反应,当他们这样做时,它要么要求批准/登录,要么只是自动登录用户。