用户OAUTH2授权完成后如何调用标记管理器数据(JavaScript)?

时间:2018-03-13 20:31:07

标签: javascript api authentication tags

我整天都在努力想弄清楚如何在授权后做一个XMLHTTP请求但是我终生难以理解。

到目前为止,我已经获得了授权用户的代码。

    var OAUTHURL    =   'https://accounts.google.com/o/oauth2/auth?';
    var VALIDURL    =   'https://www.googleapis.com/oauth2/v1/tokeninfo?
    access_token=';
    var SCOPE       =   'https://www.googleapis.com/auth/userinfo.profile 
    https://www.googleapis.com/auth/userinfo.email';
    var CLIENTID    =   'NOT SHOWING FOR SECURITY REASONS';
    var REDIRECT    =   'NOT SHOWING FOR SECURITY REASONS'
    var LOGOUT      =   'http://accounts.google.com/Logout';
    var TYPE        =   'token';
    var _url        =   OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;
    var acToken;
    var tokenType;
    var expiresIn;
    var user;
    var loggedIn    =   false;

    function login() {
        var win         =   window.open(_url, "windowname1", 'width=800, height=600'); 

        var pollTimer   =   window.setInterval(function() { 
            try {
                console.log(win.document.URL);
                if (win.document.URL.indexOf(REDIRECT) != -1) {
                    window.clearInterval(pollTimer);
                    var url =   win.document.URL;
                    acToken =   gup(url, 'access_token');
                    tokenType = gup(url, 'token_type');
                    expiresIn = gup(url, 'expires_in');
                    win.close();

                    validateToken(acToken);
                }
            } catch(e) {
            }
        }, 500);
    }

    function validateToken(token) {
        $.ajax({
            url: VALIDURL + token,
            data: null,
            success: function(responseText){  
                getUserInfo();
                loggedIn = true;
                $('#loginText').hide();
                $('#logoutText').show();
            },  
            dataType: "jsonp"  


    });
    }

    function getUserInfo() {
        $.ajax({
            url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,
            data: null,
            success: function(resp) {
                user    =   resp;
                console.log(user);
                $('#uName').text('Welcome ' + user.name);
                $('#imgHolder').attr('src', user.picture);
            },
            dataType: "jsonp"
        });
    }

    //credits: http://www.netlobo.com/url_query_string_javascript.html
    function gup(url, name) {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\#&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( url );
        if( results == null )
            return "";
        else
            return results[1];
    }

    function startLogoutPolling() {
        $('#loginText').show();
        $('#logoutText').hide();
        loggedIn = false;
        $('#uName').text('Welcome ');
        $('#imgHolder').attr('src', 'none.jpg');
    }        

就登录而言,代码工作正常。登录后我不知道该怎么做。我尝试了多种想法,却一无所获。关于如何在标签管理器中调用标签的任何想法" readonly"登录后的模式?

1 个答案:

答案 0 :(得分:0)

您好我刚刚决定尝试使用JavaScript网络应用程序方法,并且能够实现这一点。如果你使用ajax版本遇到同样的问题,这里是文档!确保选择JavaScript选项卡,或者您可以尝试oAuth2。

https://developers.google.com/identity/protocols/OAuth2UserAgent