簡體   English   中英

無法在Google Oauth中閱讀未定義的屬性'authorize'嗎?

[英]Cannot read property 'authorize' of undefined in Google Oauth?

我使用Oauth開發chrome擴展程序,用戶可以通過Google帳戶登錄。我的代碼jquery如下所示。

我還在html文件中包含了js庫:

<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>

<script src="js/client.js"></script>  

並具有JS功能:

function handleAuthClick(event) {
gapi.auth.authorize({
    client_id: clientID,
    scope: scopes,
    response_type: 'code token id_token gsession',
    access_type: accessType,
    immediate: false
}, handleAuthResult);
return false;
}

我的問題是:我無法通過谷歌帳戶登錄並顯示錯誤: Uncaught TypeError: Cannot read property 'authorize' of undefined

注意 :如果我以html身份運行,我可以正常登錄,但在用作Chrome擴展程序時我無法登錄。

似乎gapi.auth仍然沒有初始化,原因是你在進行這個調用之前沒有按照正確的步驟來設置它。 所以我只是在這里為您提供一個示例,以便在成功初始化gapi對象時查看是否缺少任何步驟。 在這里

<script type="text/javascript">
    (function( gapi ) {
        gapi.client.setApiKey(apiKey); // your variable for apiKey
        window.setTimeout(checkAuth,1);

        function checkAuth() {
            gapi.auth.authorize({client_id: clientID, scope: scopes, immediate: true},   handleAuthResult);
        }

        function handleAuthResult(authResult) {
            var authorizeButton = document.getElementById('id-of-your-login-button');
            if (authResult && !authResult.error) {
                authorizeButton.style.visibility = 'hidden';
                makeApiCall();
            } else {
                authorizeButton.style.visibility = '';
                authorizeButton.onclick = handleAuthClick;
            }
        }

      function handleAuthClick(event) {
          gapi.auth.authorize({
              client_id: clientID,
              scope: scopes,
              response_type: 'code token id_token gsession',
              access_type: accessType,
              immediate: false
          }, handleAuthResult);
          return false;
       }
     })( gapi );
</script>

您可以安全地將此腳本標記放在按鈕html代碼下方的body標記中。

暫無
暫無

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

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