OAuth 2.0 and Phonegap's InAppBrowser for Authentication

OAuth 2.0 and Phonegap's InAppBrowser for Authentication

本文关键字:InAppBrowser for Authentication and Phonegap OAuth      更新时间:2023-09-26

我正在尝试使用Oauth 2.0对登录其Google帐户的用户进行身份验证。为此,我需要访问他们的姓名和电子邮件地址。我正在使用Phonegap的InAppBrowser来做到这一点。

到目前为止,我有这个:

function openPage() {  
    var login_url = "https://accounts.google.com/o/oauth2/auth" + '?' + $.param({ client_id: "(not shown)", redirect_uri: "http://www.google.com", response_type: "token", scope: "openid profile email" });
    var loginWindow = window.open(login_url, '_blank', 'location=yes');
    loginWindow.addEventListener('loadstop', function(e) { 
        var url = e.url;
        var access_token = url.split("access_token=")[1];
        validateToken(access_token);
    });
        function validateToken(token) {
        $.ajax({
            url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
            data: null,
            success: function(responseText){  
                alert("Validation Success!");
                getUserInfo(token);
            },  
            dataType: "jsonp"  
        });
    }
    function getUserInfo(token) {
        $.ajax({
            url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + token,
            data: null,
            success: function(resp) {
                user    =   resp;
                alert(JSON.stringify(user));
            },
            dataType: "jsonp"
        });
    }
    }

令牌的验证每次都有效,但是一旦它调用getUserInfo(),返回的JSON就会充满错误和无效的凭据字符串。我做错了什么?谢谢!

我想您需要根据此处找到的用户信息端点的文档Authorization标头中设置令牌