简体   繁体   English

无法在Google Oauth中阅读未定义的属性'authorize'吗?

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

I'm use Oauth for developing for chrome extension and user can login by Google Account.I have code jquery as below. 我使用Oauth开发chrome扩展程序,用户可以通过Google帐户登录。我的代码jquery如下所示。

I'm also include js library in html file: 我还在html文件中包含了js库:

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

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

and have JS function: 并具有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;
}

My problem is: I can not login by google account and it show error : Uncaught TypeError: Cannot read property 'authorize' of undefined . 我的问题是:我无法通过谷歌帐户登录并显示错误: Uncaught TypeError: Cannot read property 'authorize' of undefined

Note : If I'm run as html I can login normally, but I can not sign in when use as chrome extension. 注意 :如果我以html身份运行,我可以正常登录,但在用作Chrome扩展程序时我无法登录。

It seems that gapi.auth is still not initialised and the reason for that is that you didn't follow the proper chain of steps to set it up before making this call. 似乎gapi.auth仍然没有初始化,原因是你在进行这个调用之前没有按照正确的步骤来设置它。 So I am just putting here a sample for you to see if you are missing any of the steps while initializing the gapi object successfully. 所以我只是在这里为您提供一个示例,以便在成功初始化gapi对象时查看是否缺少任何步骤。 Here it goes 在这里

<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>

You can safely put this script tag in your body tag just below the button html code. 您可以安全地将此脚本标记放在按钮html代码下方的body标记中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM