Skip to content

Instantly share code, notes, and snippets.

@ritou
Created March 1, 2013 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritou/5066101 to your computer and use it in GitHub Desktop.
Save ritou/5066101 to your computer and use it in GitHub Desktop.
This is Google+ Sign-In client-side sample.
<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 src="//plus.google.com/js/client:plusone.js" type="text/javascript"></script>
<script type="text/javascript">
var loginFinished = function(authResult) {
if (authResult) {
var el = document.getElementById('oauth2-results');
var label = '';
toggleDiv('oauth2-results');
if (authResult['access_token']) {
label = 'User granted access:';
gapi.auth.setToken(authResult);
} else if (authResult['error'] == 'access_denied'){
label = 'User denied access:';
} else {
label = 'An error occurred:';
}
el.innerHTML =
'<p>' + label + '</p><pre><code>' +
// JSON.stringify doesn't work in IE8.
'{"code":"' + authResult['code'] + '",\r\n' +
' "id_token":"' + authResult['id_token'] + '",\r\n' +
' "access_token":"' + authResult['access_token'] + '",\r\n' +
' "scope":"' + authResult['scope'] + '",\r\n' +
' "state":"' + authResult['state'] + '",\r\n' +
' "expires_in":"' + authResult['expires_in'] + '",\r\n' +
'}</code></pre>';
toggleDiv('signin-button');
} else {
document.getElementById('oauth2-results').innerHTML =
'Empty authResult';
}
};
function toggleDiv(id) {
var div = document.getElementById(id);
if (div.getAttribute('class') == 'hide') {
div.setAttribute('class', 'show');
} else {
div.setAttribute('class', 'hide');
}
}
</script>
</head>
<body>
<div id="signin-button" class="show">
<div class="g-signin"
data-callback="loginFinished"
data-approvalprompt="force"
data-clientid="(input your client id)"
data-requestvisibleactions="http://schemas.google.com/CommentActivity"
data-cookiepolicy="single_host_origin"
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email">
</div>
</div>
<div id="oauth2-results" class="hide"></div>
<div style="font: 12px sans-serif, Arial; margin-left: 0.5em; margin-top: 0.5em">
<a href="javascript:document.location.href=document.location.href;">Reload the example</a>
</div>
</body>
</html>
@GitTom
Copy link

GitTom commented Apr 24, 2014

This code refers to authResult['state'] - how does 'state' get into the authResult. Does the state get sent along with the request (to Google)? Or is this a different state then the 'anti-request forgery state token' that Google refers to in their docs (which js sends back to server to verify the page).

@jrichardsz
Copy link

Does not work in chrome/firefox in linux environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment