Last active
February 19, 2017 23:55
-
-
Save absent1706/9ca82dbe0d14fee346bb33131db5af6b to your computer and use it in GitHub Desktop.
Oauth2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
See https://developers.google.com/api-client-library/javascript/samples/samples#authorizing-and-making-authorized-requests | |
for description and setup instructions | |
Key setup steps: | |
1. get client ID and API KEY http://www.qopy.me/0yILY0U8TD6kZ0ilhhoIAA | |
2. run HTTP server in folder where index.html lays. | |
I.g. PHP server: | |
php -S php -S 0.0.0.0:5000 | |
or this server (just EXE) http://www.zachsaw.com/?pg=quickphp_php_tester_debugger | |
You should see this page via, say, | |
http://localhost:5000 | |
3. Register this http://localhost:5000 here http://www.qopy.me/dGS1PtL9T_i3EoI8EHOwpw , http://www.qopy.me/_tCf-zeeQeupPFK0e4Dt-Q | |
--> | |
<html> | |
<head></head> | |
<body> | |
<script type="text/javascript"> | |
function handleClientLoad() { | |
// Loads the client library and the auth2 library together for efficiency. | |
// Loading the auth2 library is optional here since `gapi.client.init` function will load | |
// it if not already loaded. Loading it upfront can save one network request. | |
gapi.load('client:auth2', initClient); | |
} | |
function initClient() { | |
// Initialize the client with API key and People API, and initialize OAuth with an | |
// OAuth 2.0 client ID and scopes (space delimited string) to request access. | |
gapi.client.init({ | |
apiKey: 'AIzaSyCiEZt9GfgrM1pADFZymMsYzCuPPJ4ZzeA', | |
discoveryDocs: ["https://people.googleapis.com/$discovery/rest?version=v1"], | |
clientId: '340024851095-s3044u17oksovrc5iqi8lcgr2abjj81b.apps.googleusercontent.com', | |
scope: 'profile' | |
}).then(function () { | |
// Listen for sign-in state changes. | |
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); | |
// Handle the initial sign-in state. | |
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); | |
}); | |
} | |
function updateSigninStatus(isSignedIn) { | |
// When signin status changes, this function is called. | |
// If the signin status is changed to signedIn, we make an API call. | |
if (isSignedIn) { | |
makeApiCall(); | |
} | |
} | |
function handleSignInClick(event) { | |
// Ideally the button should only show up after gapi.client.init finishes, so that this | |
// handler won't be called before OAuth is initialized. | |
gapi.auth2.getAuthInstance().signIn(); | |
} | |
function handleSignOutClick(event) { | |
gapi.auth2.getAuthInstance().signOut(); | |
document.getElementById('info').innerHTML = 'Logged out'; | |
} | |
function makeApiCall() { | |
// Make an API call to the People API, and print the user's given name. | |
gapi.client.people.people.get({ | |
resourceName: 'people/me' | |
}).then(function(response) { | |
console.log('Hello, ' + response.result.names[0].givenName); | |
document.getElementById('info').innerHTML = 'Hello, ' + response.result.names[0].givenName + ' '+response.result.emailAddresses[0].value; | |
}, function(reason) { | |
console.log('Error: ' + reason.result.error.message); | |
}); | |
} | |
</script> | |
<script async defer src="https://apis.google.com/js/api.js" | |
onload="this.onload=function(){};handleClientLoad()" | |
onreadystatechange="if (this.readyState === 'complete') this.onload()"> | |
</script> | |
<button id="signin-button" onclick="handleSignInClick()">Sign In</button> | |
<button id="signout-button" onclick="handleSignOutClick()">Sign Out</button> | |
<div id="info"> | |
You are not logged in | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment