我正在尝试将Google Sign In集成到我的网络应用程序中。
我的页面有Google按钮:
ModalDialogue.cshtml:
<a class="google g-signin2" data-onsuccess="onSignIn">Sign up with Google+</a>
我试图在我的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());
}
我知道
a] JavaScript在页面范围内,因为我的其他功能 - 用于打开和关闭对话 - 可以工作。
b]该按钮正在与Google通信,因为单击该按钮会打开“使用Google登录”对话框 - 然后完成后 - 将按钮更改为“登录”。
如何触发onSignIn方法?或者至少如何确定是否触发了成功?
哦。我的错。我没有注意到我把方法包裹在一个闭包中。需要全球化。
对于将来访问此页面的人,我不得不将功能定义移到按钮标记上方。看起来它应该无关紧要,因为data-onsuccess只是一个字符串。遗憾的是,它没有抛出任何错误消息。
<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