Google API OAuth 2.0 Titanium:缺少必需的参数:response_type [英] Google API OAuth 2.0 Titanium: Required parameter is missing: response_type

查看:65
本文介绍了Google API OAuth 2.0 Titanium:缺少必需的参数:response_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Titanium应用程序中获取Google的access_token来访问Google+ API.我已经在 Google API控制台中注册了Android Oauth2.0客户端.所以我有一个客户端ID和Google生成的几个重定向uris:["urn:ietf:wg:oauth:2.0:oob"," http://localhost ].我正在尝试遵循授权代码流程,因此我向端点"https://accounts.google.com/o/oauth2/v2/auth ",其中以下参数作为查询字符串:

I am trying to get an access_token from Google in a Titanium application to access the Google+ API. I have registered an Android Oauth2.0 client in the Google API Console so i have a Client ID and a couple of redirect uris generated by Google: ["urn:ietf:wg:oauth:2.0:oob","http://localhost"]. I am trying to follow the authorization code flow, so i have made an authorization request to the endpoint "https://accounts.google.com/o/oauth2/v2/auth" with the following parameters as query strings:

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并使用appendend查询字符串重定向到授权端点.Google登录屏幕打开,我可以登录并授予对该应用程序的访问权限.作为回报,我得到一个嵌入了授权代码的URL,可以提取该URL以用于下一个呼叫.

Then i create a webview and redirect to the authorization endpoint with the appendend query strings. The google login screen opens and i can login and grant access to the application. In return i get an url with the authorization code embedded which i can extract to use for the next call.

要获取access_token,我向" https://accounts.google发出POST请求.com/o/oauth2/v2/auth "端点.这是功能:

To get the access_token i make a POST request to "https://accounts.google.com/o/oauth2/v2/auth" endpoint. This is the function:

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".

But i got a status code of 400 with the following message: "Required parameter is missing: response_type".

我不确定为什么要这样做,因为来自 OAuth 2.0规范访问令牌请求所需的参数仅为grant_type,代码,client_id和redirect_uri.我还尝试添加response_type ="token",但是如果我理解正确的话,那应该用于隐式流程.

I am not sure why i am getting this, since from OAuth 2.0 specification the required parameters for the access token request are just grant_type, code, client_id and redirect_uri. I have also tried to add response_type = "token" but that should be for the implicit flow if i understand correctly.

有什么建议吗?

推荐答案

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

It seems i found the problem, the endpoint for the token exchange is not correct. It should be "https://accounts.google.com/o/oauth2/token", at least this one worked for me.

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

I would point out that in the latest documentation of Google the endpoint for the token exchange is this one: "https://accounts.google.com/o/oauth2/v2/token" but for some reason it doesn't work for me (the response says that the url is not supported by the server). Hope this can help people with similar issue.

这篇关于Google API OAuth 2.0 Titanium:缺少必需的参数:response_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆