I know similar questions, but still have a problem:
I also understand that the callback sign is called initially even without a request, in order to check if the user is registered elsewhere. The "immediate error" function also returns correctly when a user logs out of a browser from other Google services. However, when the user is actually registered in Gmail in a different tab, I still get the same javascript rejection.
This is a simple Google login code example. What could be wrong? Some information:
Credentials:
Redirect URIs http://localhost:8000/beta/oauth2callback
Javascript Origins http://localhost:8000
Corresponding code (only Javascript is registered, copied and only slightly modified: https://developers.google.com/+/web/signin/add-button )
Button Announcement:
<div class="g-signin" data-callback="loginFinished"
data-clientid="268583......"
data-scope="profile email"
data-cookiepolicy="single_host_origin"
>
Callback:
var loginFinished = function(authResult) {
console.log(authResult)
if (authResult['code']) {
var el = document.getElementById('oauth2-results');
var label = '';
toggleDiv('oauth2-results');
if (authResult['status']['signed_in']) {
label = 'User granted access:';
gapi.auth.setToken(authResult);
} else {
label = 'Access denied: ' + authResult['error'];
}
el.innerHTML =
label + '<pre class="prettyprint"><code>' +
'}</code></pre>';
toggleDiv('signin-button');
} else {
document.getElementById('oauth2-results').innerHTML =
'Error';
}
};
Full code (served locally by Apache at: 8000 / test0 / signin_demo_basic.htm)
<html>
<head>
<title>Google+ Sign-in button demo</title>
<style type="text/css">
html, body { margin: 0; padding:0;}
#signin-button {
padding: 5px;
}
#oauth2-results pre { margin: 0; padding:0;}
.hide { display: none;}
.show { display: block;}
</style>
<script type="text/javascript">
var loginFinished = function(authResult) {
console.log(authResult)
if (authResult['code']) {
var el = document.getElementById('oauth2-results');
var label = '';
toggleDiv('oauth2-results');
if (authResult['status']['signed_in']) {
label = 'User granted access:';
gapi.auth.setToken(authResult);
} else {
label = 'Access denied: ' + authResult['error'];
}
el.innerHTML =
label + '<pre class="prettyprint"><code>' +
'{<br />' +
' "id_token" : "' + authResult['id_token'] +'",<br />' +
' "access_token" : "' + authResult['access_token'] + '",<br />' +
' "state" : "' + authResult['state'] + '",<br />' +
' "expires_in" : "' + authResult['expires_in'] + '",<br />' +
' "error" : "' + authResult['error'] + '",<br />' +
' "error_description" : "' + authResult['error_description'] + '",<br />' +
' "authUser" : "' + authResult['authuser'] + '",<br />' +
' "status" : {"' + '<br />' +
' "google_logged_in" : "' + authResult['status']['google_logged_in'] + '",<br />' +
' "method" : "' + authResult['status']['method'] + '",<br />' +
' "signed_in" : "' + authResult['status']['signed_in'] + '"<br />' +
' }<br />' +
'}</code></pre>';
toggleDiv('signin-button');
} else {
document.getElementById('oauth2-results').innerHTML =
'Error';
}
};
function toggleDiv(id) {
var div = document.getElementById(id);
if (div.getAttribute('class') == 'hide') {
div.setAttribute('class', 'show');
} else {
div.setAttribute('class', 'hide');
}
}
</script>
<script src="https://plus.google.com/js/client:platform.js" type="text/javascript"></script>
</head>
<body>
<div id="signin-button" class="show">
<div class="g-signin" data-callback="loginFinished"
data-clientid="268583......"
data-scope="profile email"
data-cookiepolicy="single_host_origin"
>
</div>
</div>
<div id="oauth2-results" class="hide"></div>
<div><a href="javascript:document.location.reload();">Reload the example</a> or <a
href="/+/demos/signin_demo_basic" target="_blank">open in a new window</a></div>
</body>
</html>