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

查看:101
本文介绍了页面刷新后,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.

作为一个成功方案的示例,首次加载页面时,如果用户已经登录,则调用true分配给authInstance.isSignedIn.listen的回调.

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.

这是我自己的消息来源:

Here's my own source:

<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天全站免登陆