我正在尝试从我的网站实施“使用Google登录”按钮。
我尝试了两种javascript:
<script>
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'MY_CLIENT_ID.apps.googleusercontent.com',
fetch_basic_profile: false,
scope: "email" //I do only need the email for the moment
});
auth2.signIn().then(function() {
console.log(auth2.currentUser.get().getId());
});
});
</script>
和“非JavaScript”:
<meta name="google-signin-scope" content="email">
<meta name="google-signin-client_id" content="MY_CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
function google(googleUser) {
var profile = googleUser.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());
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
}
function fail(error) {
console.log(error);
}
</script>
<div class="g-signin2" data-onsuccess="google" data-onfailure="fail" data-theme="dark" data-longtitle="true"></div>
版。
但是在两种情况下,我都会收到此错误(作为第一个脚本中的错误以及第二个脚本中fail()的日志):
TypeError: this.u3.open is not a function
at fv.open (cb=gapi.loaded_0:250)
at gw (cb=gapi.loaded_0:298)
at Dx (cb=gapi.loaded_0:341)
at $w (cb=gapi.loaded_0:337)
at xx.<anonymous> (cb=gapi.loaded_0:338)
at new _.rk (cb=gapi.loaded_0:188)
at xx.Ej (cb=gapi.loaded_0:338)
at bx.a.<computed> [as signIn] (cb=gapi.loaded_0:321)
at login:269
at platform.js:28
我已经在API开发人员控制台中将我的域列入了白名单...我在做什么错?
通过添加ux_mode: "redirect"
到initializator / meta标签解决
它变成这样的东西:
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'MY_CLIENT_ID.apps.googleusercontent.com',
fetch_basic_profile: false,
scope: "email",
ux_mode: "redirect"
});
auth2.signIn().then(function() {
console.log(auth2.currentUser.get().getId());
});
});
问题是,现在我不能打电话success()
的fail()
。
我知道这是题外话,但在评论中或此处的任何帮助将不胜感激!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句