谷歌API OAuth 2.0 钛金:缺少必选参数:response_type

5

我正在尝试在Titanium应用程序中从Google获取access_token以访问Google+ API。 我已在Google API控制台注册了一个Android Oauth2.0客户端,因此我拥有由Google生成的客户端ID和一些重定向uris:["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并使用附加的查询字符串重定向到授权端点。Google登录界面打开,我可以登录并授权应用程序。作为回报,我会得到一个包含授权代码的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规范,访问令牌请求所需的参数只有grant_type、code、client_id和redirect_uri。我也尝试添加response_type = "token",但如果我理解正确,那应该是针对隐式流程的。有什么建议吗?
2个回答

3
我曾经遇到过同样的问题,文档中并没有提及正确的URL。但我发现,当你注册一个客户端时,JSON结果文件包含client_id和client_secret,也会包含auth_uri和token_uri。
希望这能帮助到某些人 :)

这应该是被接受的答案。client_secret_*.json文件包含URL信息。 - viggy28

2

1
这个页面上,我找到了另一个适合我的URL:https://www.googleapis.com/oauth2/v4/token - André Laszlo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接