Skip to content

Instantly share code, notes, and snippets.

@tsabunkar
Created June 15, 2018 11:38
Show Gist options
  • Save tsabunkar/578e8b019c1f67cea04cb53d6da1a77b to your computer and use it in GitHub Desktop.
Save tsabunkar/578e8b019c1f67cea04cb53d6da1a77b to your computer and use it in GitHub Desktop.
GoogleAPI Sigin - Its only for webapplication (frontend) which has been generated using OAuth client ID.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="google-signin-client_id" content="306552833988-0pnfe2jga10fb1orkpa8gu08pm3bl14p.apps.googleusercontent.com">
<title>Home Page</title>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
</body>
</html>
@tsabunkar
Copy link
Author

Note: While creating this client id, In Authorized JavaScript origins need to give url where you will be running this clientId for ex- http://localhost:3000 for deployment environment. And also enable Google+ API in API Library.

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