【问题标题】:Google API OAuth 2.0 Titanium: Required parameter is missing: response_typeGoogle API OAuth 2.0 Titanium:缺少必需参数:response_type
【发布时间】:2016-12-14 14:00:40
【问题描述】:

我正在尝试在 Titanium 应用程序中从 Google 获取 access_token 以访问 Google+ API。 我在Google API Console注册了一个Android Oauth2.0客户端 所以我有一个客户端 ID 和谷歌生成的几个重定向 uri:[“urn:ietf:wg:oauth:2.0:oob”、“http://localhost”]。 我正在尝试遵循授权代码流程,因此我使用以下参数作为查询字符串向端点“https://accounts.google.com/o/oauth2/v2/auth”发出了授权请求:

client_id = encodeURI(<app id>)
redirect_uri = encodeURI("urn:ietf:wg:oauth:2.0:oob")
response_type = "code",
state = <random generated number>
scope = "https://www.googleapis.com/auth/plus.me"

然后我创建一个 webview 并使用附加查询字符串重定向到授权端点。谷歌登录屏幕打开,我可以登录并授予对应用程序的访问权限。作为回报,我得到一个嵌入了授权码的 url,我可以提取它以用于下一次调用。

为了获取 access_token,我向“https://accounts.google.com/o/oauth2/v2/auth”端点发出 POST 请求。这是函数:

function getAccessToken(code) {

Ti.API.warn("Authorization code: " + code);
    
var auth_data = {
    code : code,
    client_id : client_id,
    redirect_uri : redirect_uri,
    grant_type : "authorization_code",
};


var client = Ti.Network.createHTTPClient({
    
    onload: function() {
        var response_data = JSON.parse(this.responseText);
        var access_token = response_data["access_token"];
        var expires_in = response_data["expires_in"];
    },
    
    
    onerror: function() {
        Ti.API.error("HTTPClient: an error occurred.");
        Ti.API.error(this.responseText);
    }
    
});

var body = "";
    
for (var key in auth_data) {
    if (body.length) {
        body += "&";
    }
    body += key + "=";
    body += encodeURIComponent(auth_data[key]);
}

client.open("POST", "https://accounts.google.com/o/oauth2/v2/auth");
client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
client.send(body);


}

但我得到了 400 的状态码,并显示以下消息:“缺少必需的参数:response_type”。

我不知道为什么我会得到这个,因为来自OAuth 2.0 specification 的访问令牌请求所需的参数只是grant_type、code、client_id 和redirect_uri。我也尝试添加 response_type = "token" 但如果我理解正确,那应该是用于隐式流程。

有什么建议吗?

【问题讨论】:

    标签: mobile oauth google-api authorization titanium


    【解决方案1】:

    似乎我发现了问题,令牌交换的端点不正确。 应该是“https://accounts.google.com/o/oauth2/token”,至少这个对我有用。

    我要指出的是,在 Google 的最新文档中,令牌交换的端点是这个:“https://accounts.google.com/o/oauth2/v2/token”但由于某种原因它对我不起作用(响应说不支持 url由服务器)。希望这可以帮助有类似问题的人。

    【讨论】:

    • this page 我找到了另一个适合我的网址:https://www.googleapis.com/oauth2/v4/token
    【解决方案2】:

    我遇到了同样的问题,documentation 没有提到正确的 URL。 但是我注意到,当你注册一个客户端时,包含client_id和client_secret的JSON结果文件也包含auth_uri和token_uri。

    我希望这会对某人有所帮助:)

    【讨论】:

    • 这应该是公认的答案。 client_secret_*.json 文件包含 URL 信息。
    猜你喜欢
    • 2015-07-13
    • 2012-01-02
    • 2015-09-29
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多