谷歌认证javascript

谷歌认证javascript,javascript,google-api,google-api-client,google-authentication,Javascript,Google Api,Google Api Client,Google Authentication,我正在尝试在我们的网站上实现谷歌登录。我已经阅读了文档并在API控制台上设置了一个应用程序 我更喜欢在弹出窗口中显示注册对话框,在用户登录并接受我将获得javascript回调的权限后显示。根据文档,api也支持这一点。因此,我在文档的帮助下构建了以下内容;-) 第一部分是加载google客户端脚本async和init,并使用正确的clientid和apikey加载脚本 下一部分是使用google客户端api的部分。加载client.js时调用handleClientLoad()。该方法检查使用

我正在尝试在我们的网站上实现谷歌登录。我已经阅读了文档并在API控制台上设置了一个应用程序

我更喜欢在弹出窗口中显示注册对话框,在用户登录并接受我将获得javascript回调的权限后显示。根据文档,api也支持这一点。因此,我在文档的帮助下构建了以下内容;-)

第一部分是加载google客户端脚本async和init,并使用正确的clientid和apikey加载脚本

下一部分是使用google客户端api的部分。加载client.js时调用handleClientLoad()。该方法检查使用是否经过身份验证。如果用户是,我的想法是要登录用户。 如果用户尚未通过身份验证,则会出现一个按钮,在调用单击handleAuthClick()时,该按钮与handleClientLoad()的操作相同,但会出现一个弹出窗口,其中用户最多登录(使用google帐户)并接受权限。登录后调用handleAuthResult()以登录用户

那么现在来看看问题:

  • 调用handleClientLoad时,用户甚至从未经过身份验证 如果用户已经自动通知
  • 如果用户使用handleAuthClick(),弹出窗口将显示登录、权限和权限 回调到handleAuthResult()工作。但是参数authResult是 总是什么都没有(应该是符合文档的东西)
  • 如果我多次尝试而不重新加载页面,有时我可以 让makeApiCall()和gapi.auth.getToken()工作并获取我需要的信息
    代码中有两个问题:

    • API密钥不是必需的,您可以将其删除。您可以通过OAuth2获得用户令牌,这就足够了
    • authorize()
      中,未正确调用
      handleAuthResult
      方法,请删除函数名末尾的括号。您不想执行该函数,只需传递其引用即可。下面是
      authorize
      方法的外观:

      gapi.auth.authorize({client\u id:this.clientId,scope:this.scopes,immediate:immediate},this.handleAuthResult)


    请注意括号中的差异。

    我在使用javascript API时也遇到同样的问题。如果你已经解决了问题,请分享你的答案。
    $gp = new googlePlus('@Trustpilot.Web.Social.Google.ClientID', '@Trustpilot.Web.Social.Google.ApiKey');
    (function () {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/client.js?onload=googlePlusClientLoad';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
    
    function googlePlus(clientId, apiKey) {
        this.clientId = clientId;
        this.apiKey = apiKey;
        this.scopes = 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email';
    
        /// This method is called when the javascript is loaded async
        this.handleClientLoad = function() {
            gapi.client.setApiKey(this.apiKey);
            window.setTimeout(this.authorize(true), 1);
        };
    
    this.handleAuthResult = function (authResult) {
        console.log(authResult);
        if (authResult && !authResult.error) {
            var token = gapi.auth.getToken();
            console.log(token);
        }
        else if (authResult && authResult.error) {
            alert(authResult.error);
        }
    };
    
    this.handleAuthClick = function(event) {
        this.authorize(false);
        return false;
    };
    
    this.makeApiCall = function() {
        gapi.client.load('plus', 'v1', function () {
            var request = gapi.client.plus.people.get({
                'userId': 'me'
            });
            request.execute(function (resp) {
                console.log(resp);
            });
        });
    };
    
        this.authorize = function (immediate) {
            gapi.auth.authorize({ client_id: this.clientId, scope: this.scopes, immediate: immediate }, this.handleAuthResult());
            //gapi.auth.authorize({ client_id: this.clientId, scope: this.scopes, immediate: immediate }, this.handleAuthResult());
        };
    }
    var googlePlusClientLoad = function() {
        $gp.handleClientLoad();
    };