页面刷新后,gapi 登录状态未保留在用户浏览器中 [英] gapi login state not retained in users browser after page refresh

查看:26
本文介绍了页面刷新后,gapi 登录状态未保留在用户浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我投入了一个旨在利用 YouTube API 的小项目.我已经准备好了一些基本代码,我最初认为这些代码可以正常工作.

I've dove into a small project aiming to utilize the YouTube API. I've got some basic code in place, that I initially thought was, working properly.

使用 Chrome,我可以使用以下源代码通过我自己网络上的多台机器登录而不会出现任何问题.我还将我的机器连接到我的蜂窝网络,以确保该过程在我自己的网络之外也能正常工作.

Using Chrome, I can login through multiple machines on my own network without any issues using the source below. I've also tethered my machines to my cellular network to ensure that the process also works properly outside of my own network.

不幸的是,我在找出一位同事(来自远程位置)在成功通过 Chrome 登录后无法在页面刷新之间保持登录状态的原因时遇到问题,尽管没有明显问题使用 Firefox 时.

Unfortunately, I'm having issues finding out why a colleague (from a remote location) is unable to retain his login state between page refreshes after successfully logging in through Chrome, although no issues are apparent when using Firefox.

举一个成功场景的例子,当第一次加载页面时,分配给authInstance.isSignedIn.listen的回调被调用,值为true如果用户已经登录.

For an example of a successful scenario, when loading the page for the first time, the callback assigned to authInstance.isSignedIn.listen is called with a value of true if the user has already logged in.

在我同事的机器上,登录成功后刷新页面没有调用authInstance.isSignedIn.listen监听器,说明没有登录.

On my colleagues machine, the authInstance.isSignedIn.listen listener is not called after refreshing the page after successfully logging in, indicating no login.

我目前没有使用 SSL 证书,如果该信息有帮助的话.

I'm currently not using an SSL certificate, if that information helps any.

其他信息包括我的同事能够在支持 YouTube 身份验证的其他网站上进行身份验证和保持登录状态而不会出现任何问题.我一直无法查看这些网站的来源,因为来源已被混淆.

Additional information includes my colleagues ability to authenticate and retain login state without any issues on other sites that support YouTube authentication. I've been unable to look through the source of those sites, as the source is obfuscated.

这是我自己的来源:

<html lang="en">
<head>
    <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</head>
<body>
    <button id="sign-in">Sign In</button>
    <button id="sign-out">Sign Out</button>
    <script>
        var authInstance;
        var signInBtn = document.getElementById('sign-in');
        var signOutBtn = document.getElementById('sign-out');

        function onLoad() {
            gapi.load('auth2', function() {
                console.log('loadAuth2()');
                auth2 = gapi.auth2.init({
                    client_id: '[CLIENT_ID]',
                });

                authInstance = gapi.auth2.getAuthInstance();
                authInstance.isSignedIn.listen(function () {
                    console.log('authUpdate()');
                    console.log(arguments);
                });
            });
        }

        signInBtn.onclick = function () {
            console.log('signInBtn onclick()');

            /*

            Signing in this way produces the same results.

            auth2.grantOfflineAccess({
              scope: "https://www.googleapis.com/auth/youtube.readonly",
              redirect_uri: "postmessage"
            }).then(function () {
                console.log("accessGranted() (?)");
                console.log(arguments);
            });

            */

            auth2.signIn({
                scope: "https://www.googleapis.com/auth/youtube",
            }).then(function () {
                console.log('signedIn()');
                console.log(arguments);
            });
        };

        signOutBtn.onclick = function () {
            console.log('signOutBtn onclick()');
            authInstance.signOut();
        };
    </script>
</body>
</html>

推荐答案

我不是 Google 登录方面的专家,但根据我的经验,以下代码不起作用.

I'm not an expert in Google Sign-In, in my experience, however, the following code did not work.

authInstance = gapi.auth2.getAuthInstance();
console.log(authInstance.isSignedIn.get());

相反,你应该这样做:

authInstance = gapi.auth2.getAuthInstance();
authInstance.then(function() {  // onInit
  console.log(authInstance.isSignedIn.get());
}, function() {  // onError
});

您应该等到 GoogleAuth 对象完全初始化.

You should wait until the GoogleAuth object is fully initialized.

这篇关于页面刷新后,gapi 登录状态未保留在用户浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆