我正在尝试将Google签名按钮集成到我的网络应用程序中。这是我第一次这样做,而且我在控制台日志中不断收到以下消息......
未定义auth2
此外,当我刷新页面时,谷歌按钮显示“登录”而不是“登录”
以下是我的代码。谢谢!
<script>
gapi.load('auth2', function () {
auth2 = gapi.auth2.init();
// Sign the user in, and then retrieve their ID.
auth2.signIn().then(function () {
console.log(auth2.currentUser.get().getId());
});
});
if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
console.log('ID: ' + profile.getId());
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());
}
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>
答案 0 :(得分:0)
不要让Google API误导你。错误可能是因为这一行(并且代码在(&#39;严格模式&#39;)中:
auth2 = gapi.auth2.init();
因为错误表明&#34; auth2&#34;未定义(在严格模式下,不允许在全局空间中创建变量)。声明如下:
var auth2 = gapi.auth2.init();
同样的事情发生在gapi auth2 init failing with "Uncaught ReferenceError: auth2 is not defined"。