使用javascript安装应用的Google Oauth

时间:2013-11-20 07:11:44

标签: javascript android jquery oauth google-oauth

我想在我的android或tizen设备中使用javascript来做Google Oauth。我不想使用手机间隙或其他图书馆,我使用jquery。我可以在手机的浏览器中获取sigin页面,在接受权限后,浏览器会将我重定向到一个网页,说要复制此代码并粘贴到您的application.sucess代码ex。“4 / wxtgskd_jsdskeehd”。但我不知道从这里做了什么。如何复制此代码或如何从这里完整oauth。或者如何从浏览器返回我的应用程序。 以下是oauth的完整代码。

<html>
<head>
   <script src="js/lib/jquery-2.0.3.min.js"></script>
    <script>
        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    =   '103321196121-1adn1lgikd9sepmk6kp4lel1i72fhf96.apps.googleusercontent.com';
        var REDIRECT    =   'urn:ietf:wg:oauth:2.0:oob'
        var LOGOUT      =   'http://accounts.google.com/Logout';
        var TYPE        =   'code';
        var _url        =   OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;
        var acToken;
        var tokenType;
        var expiresIn;
        var user;
        var code;
        var loggedIn    =   false;

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

            var pollTimer   =   window.setInterval(function() { 
                try {

                    if (win.document.URL.indexOf(REDIRECT) != -1) {

                        window.clearInterval(pollTimer);

                        var url =   win.document.URL;
                        code= gup(url, 'code');
                        console.log(url);
                        acToken =   gup(url, 'access_token');
                        tokenType = gup(url, 'token_type');
                        expiresIn = gup(url, 'expires_in');
                        win.close();

                        validateToken(acToken);
                    }
                } catch(e) {
                }
            }, 500);
             alert(win.document.URL);
        }

        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');
        }

    </script>
</head>

<body>
    <a href='#' onClick='login();' id="loginText"'> Click here to login </a>
    <a href="#" style="display:none" id="logoutText" target='myIFrame' onclick="myIFrame.location='https://www.google.com/accounts/Logout'; startLogoutPolling();return false;"> Click here to logout </a>
    <iframe name='myIFrame' id="myIFrame" style='display:none'></iframe>
    <div id='uName'></div>
    <img src='' id='imgHolder'/>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你应该使用'token'(而不是'code')response_type。

令牌流将访问令牌直接放在URL哈希中,而代码流将返回一个授权,以便您可以从令牌端点获取访问令牌。