javascript 为 Google 登录调用数据成功方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31733246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling data-onsuccess method for Google sign-in
提问by Dave Collins
I am trying to integrate Google Sign Ininto my web app.
我正在尝试将Google Sign In集成到我的网络应用程序中。
My page has the Google button:
我的页面有 Google 按钮:
ModalDialogue.cshtml:
<a class="google g-signin2" data-onsuccess="onSignIn">Sign up with Google+</a>
I'm trying to trigger that method in my js:
我正在尝试在我的 js 中触发该方法:
ModalDialogue.js:
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());
}
I know that
我知道
a] the JavaScript is in the scope of the page, since my other functions - for opening and closing the dialogue - DO work.
a] JavaScript 在页面的范围内,因为我的其他功能 - 用于打开和关闭对话 - 可以工作。
b] the button is communicating with Google, since clicking on it DOES open the 'sign-in with Google' dialogue - and then once done - changes the button to 'Sign In' afterward.
b] 该按钮正在与 Google 通信,因为点击它会打开“使用 Google 登录”对话框 - 然后一旦完成 - 之后将按钮更改为“登录”。
How do I trigger the onSignIn method? Or at least how do I determine whether onsuccess is being triggered?
如何触发 onSignIn 方法?或者至少我如何确定是否触发了 onsuccess ?
回答by DaveC426913
Oh. My bad. I hadn't noticed I had the method wrapped in a closure. Needs to be global.
哦。我的错。我没有注意到我将方法包裹在闭包中。需要全球化。
回答by bradlis7
For people who come to this page in the future, I had to move the function definition above the button tag. Seems like it shouldn't matter, as data-onsuccess is simply a string. It didn't throw any error messages, unfortunately.
对于以后来这个页面的人,我不得不将函数定义移到按钮标签上方。似乎这无关紧要,因为 data-onsuccess 只是一个字符串。不幸的是,它没有抛出任何错误消息。
回答by Prakash Choudhary
<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
}
</script>
</body>
</html>
for for infofor info
为信息为信息
回答by Subham choudhary
enable third party cookie in your browser
在您的浏览器中启用第三方 cookie