簡體   English   中英

(Javascript)網站無法正常使用Google登錄

[英](Javascript) Google Sign-In for websites not working

我將其放在<head>部分:

<!-- Google Authentication -->
<script async defer src="https://apis.google.com/js/api.js?"></script>

<script type="text/javascript">
    var auth2;
    var initClient = function() {
        gapi.load('auth2', function(){
            auth2 = gapi.auth2.init({
                client_id: '*********.apps.googleusercontent.com',
                fetch_basic_profile : false,
                scope : 'profile'
            });
            auth2.then(function(){
                if (auth2.isSignedIn.get() == true) {
                    auth2.signIn();
                }
            })
            auth2.then(function(){
              if (!auth2.isSignedIn.get()) {
                    location.href("index.html")
              }
        }
        });
    };
</script>

但是,從未到達“ location.href(“ index.html”)。有人知道為什么嗎?

如果我將location.href(...)放在.init(...)函數上方,它將可以到達。 但是,如果我將其放在下面,它將無法正常工作。 所以錯誤是在.init()函數中,但我找不到它是什么

(auth2.isSignedIn.get()的結果為“未定義”,它應該為true或false)

替換您的代碼:

<script type="text/javascript">
    var auth2;
    var initClient = function() {
        gapi.load('auth2', function(){
            auth2 = gapi.auth2.init({
                client_id: '*********.apps.googleusercontent.com',
                fetch_basic_profile : false,
                scope : 'profile'
            });
            auth2.then(()=>{
                if (auth2.isSignedIn.get() == true) {
                    auth2.signIn();
                } else{
                     window.location.href="index.html";
                  }
            })

        }
        });
    };
</script>

解決了

<script src="https://apis.google.com/js/platform.js?onload=initLoad" async defer></script>
<script>
    try{
        window.initLoad = function(){
            gapi.load('auth2', function(){
                var auth2 = gapi.auth2.init({
                    client_id: '********.apps.googleusercontent.com'
                });
                if (auth2.isSignedIn.get() == true) {
                    auth2.signIn();
                }else{
                    window.location.assign("index.html");
                }
            });
        }
    }catch(err){
        console.log(err.message);
    }
</script>

現在我有一個新問題,“登錄”按鈕不再起作用...在另一頁上,除了好,我整天都沒有碰其他任何問題...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM