gpt4 book ai didi

google-signin - Google SignIn 登录脚本不起作用

转载 作者:行者123 更新时间:2023-12-01 17:56:23 29 4
gpt4 key购买 nike

我使用了以下脚本来集成谷歌身份验证

//google auth code
gapi.load('auth2', function() {//load in the auth2 api's, without it gapi.auth2 will be undefined
gapi.auth2.init(
{
client_id: '1063305095705-k68gpfhi46tbu6kv8e8tidc751hte5pb.apps.googleusercontent.com'
}
);
var GoogleAuth = gapi.auth2.getAuthInstance();//get's a GoogleAuth instance with your client-id, needs to be called after gapi.auth2.init
$scope.onLogInButtonClick=function(){//add a function to the controller so ng-click can bind to it
GoogleAuth.signIn().then(function(response){//request to sign in
console.log(response);
console.log(response.wc.wc);
console.log(response.wc.hg);
console.log(response.wc.Ph);
});
};
});

我尝试在其他应用程序中使用它并收到以下错误:-

gapi.auth2.ExternallyVisibleError: gapi.auth2 has been initialized with
different options. Consider calling gapi.auth2.getAuthInstance()
instead of gapi.auth2.init().

我还包括

<script src="https://apis.google.com/js/platform.js?key=xxxxxxxxx"></script>

一切似乎都很好,不明白为什么它不起作用..

最佳答案

接下来的代码应该有帮助:我花了两个小时来解决这个问题,因为我收到了同样的错误,所以我决定使用 onLoad 函数和自定义谷歌登录按钮。

如果我们不想从用户那里获取个人资料信息,那么一些要点可能是必要的......

  • 使用 onload 函数,如下代码所示,当使用 defer 异步方法时:

https://apis.google.com/js/platform.js?onload=onLoadCallback

  • 使用登录功能上的点击事件制作自己的登录按钮
<a onclick="signIn();">Google Sign In</a>

完整的解决方案在这里:

<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>

<meta name="google-signin-client_id" content="YOUR_ID.apps.googleusercontent.com"/>

<!-- I have commented default google sign in button because it didn't allow me to make my own gapi.load -->
<!--<div class="g-signin2" data-onsuccess="ConSignIn"></div>-->

<a onclick="signIn();">Google Sign In</a>

<script>

function onLoadCallback() {

console.log('onLoadCallback');
gapi.load('auth2', function() {
gapi.auth2.init({
client_id: 'YOUR_ID.apps.googleusercontent.com',
//This two lines are important not to get profile info from your users
fetch_basic_profile: false,
scope: 'email'
});
});
}

function signIn() {
console.log('I am passing signIn')

var auth2 = gapi.auth2.getAuthInstance();
// Sign the user in, and then retrieve their ID.
auth2.signIn().then(function() {
console.log(auth2.currentUser.get().getId());
});
}

</script>

关于google-signin - Google SignIn 登录脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067867/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com