Ale*_*haw 5 html javascript google-signin google-identity
我一直在关注Google登录教程,以创建一个登录系统,而此时我有一个登录按钮,该按钮会加载用于选择登录的小部件。单击后,将加载以下功能:
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());
}
Run Code Online (Sandbox Code Playgroud)
但是,一旦我选择了帐户并登录,就什么也没有发生。在开发人员控制台中,当我输入“ googleUser”时,它只会显示:
未捕获的ReferenceError:未定义googleUser
这是我所有的代码:
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());
}
Run Code Online (Sandbox Code Playgroud)
请注意,运行代码段将不起作用
我遇到了同样的问题,并且使用Jaros?aw的技巧设法解决了这个问题。
解决方案:
function onSignIn() {
const googleUser = gapi.auth2.getAuthInstance().currentUser.get();
const profile = googleUser.getBasicProfile();
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
尝试添加额外的元:
<meta name="google-signin-client_id" content="451815931731-u24ino5uecga1arf65814fd72201fcmu.apps.googleusercontent.com">
<meta name="google-signin-cookiepolicy" content="single_host_origin" />
<meta name="google-signin-requestvisibleactions" content="https://schema.org/AddAction" />
<meta name="google-signin-scope" content="https://www.googleapis.com/auth/plus.login" />
Run Code Online (Sandbox Code Playgroud)
并确保在您的 google Api Manager、凭据、OAuth 2.0 客户端 ID 中,检查您的授权 JavaScript 来源,确保您输入页面运行的来源站点。如果您在本地主机中开发,请添加http://127.0.0.1作为您的起源脚本。