Reputation: 11
I´m using google login javascript.
I want import js after click button. The problem: Only show popup login google the second time I click the button
My Code:
function clickButton(){
$.getScript( "https://apis.google.com/js/client:plusone.js?onload=onLoadGoogleLogin");
}
function onLoadGoogleLogin()
{
var initializedGoogleCallback = false;
gapi.client.setApiKey('AIzaSyBhV6vP_D_I7ldL1YIU7LJUGtSxJ55ievw');
var myParams = {
'callback': function googleLoginCallback(result) {
debugger;
if (!initializedGoogleCallback) {
if (result) {
if (result['error'] == undefined) {
initializedGoogleCallback = true;
gapi.auth.setToken(result);
gapi.client.load('oauth2', 'v2', function () {
var request = gapi.client.oauth2.userinfo.get();
request.execute(googleLoginDataUserCallback);
});
}
} else {
alert('Empty authResult');
}
}
},
'clientid': 'xxxxx-4kragpsm6jolann6d9t74crpkthch0iq.apps.googleusercontent.com',
'cookiepolicy': 'single_host_origin',
'requestvisibleactions': 'http://schemas.google.com/AddActivity',
'scope': 'https://www.googleapis.com/auth/plus.login'
}
Upvotes: 0
Views: 1088
Reputation: 11
Getting Started with Google Sign-In OAuth: A Step-by-Step Guide for Web Developers
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Google Sign in</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
function onSignIn(googleUser) {
const responsePayload = decodeJwtResponse(googleUser.credential);
let google_email = responsePayload.email
console.log("google_email - ",google_email)
}
function decodeJwtResponse(data) {
let tokens = data.split(".");
return JSON.parse(atob(tokens[1]))
}
</script>
</head>
<body>
<div class="login">
<div id="g_id_onload" data-client_id="enter-your-client-id"
data-context="signin" data-ux_mode="popup" data-login_uri="" data-callback="onSignIn"
data-itp_support="true">
</div>
<div class="g_id_signin" data-type="standard" data-shape="rectangular" data-theme="outline" data-text="signin"
data-size="large" data-logo_alignment="left">
</div>
</div>
</div>
</body>
</html>
find out code : GitHub - https://github.com/Sravankumarraju/googleSignin
Upvotes: 1