我正在使用 Cordova 的 inappbrowser 并集成 Google oauth2.0。获得授权代码后,我会发出发布请求来获取我的令牌。无论我尝试什么,我总是收到 400 错误,提示“必需参数缺少 grant_type”。我正在编码 uri,我正在设置正确的标头,但无济于事......任何人都可以帮忙吗?
$http({
method: 'POST',
url: 'https://accounts.google.com/o/oauth2/token',
params:{code:authorization_code[0],
client_id:options.client_id,
client_secret:options.client_secret,
redirect_uri:options.redirect_uri,
grant_type:'authorization_code'},
headers:{
'Content-Type':'application/x-www-form-urlencoded',
}
}).success(function(data,status,headers,config){
deferred.resolve(data);
}).error(function(data, status,headers,config){
console.log('data, status, headers,config',data,status,headers,config);
deferred.reject(response.responseJSON);
});
这是当我尝试发出请求时 Chrome 开发控制台的输出
请求网址:
请求方式:POST
状态代码:400 错误请求
请求标头
POST HTTP/1.1
Accept:应用程序/json、文本/纯文本、/
来源:文件://
测试:测试
用户代理:Mozilla/5.0(Linux;Android 4.4.2;SCH-I535 Build/KOT49H)AppleWebKit/537.36(KHTML,如 Gecko)版本/4.0 Chrome/30.0.0.0 Mobile Safari/537.36
请求参数参数
client_id=xxx-oh7o4cmaju3jgprllln97nf0p3pc1f91.apps.googleusercontent.com&client_secret=xxx&code=4%2FnITDK731NhavPePthrVA1eX8LHFC.ojUX9K7DpBYaEnp6UAPFm0HWDS5njgI&grant_type=authorization_code&redirect_uri=http:% 2F%2F本地主机
响应头HTTP/1.1 400 错误请求
编译指示:无缓存
日期:2014 年 7 月 14 日星期一 06:35:22 GMT
内容编码:gzip
X-内容类型选项:nosniff
服务器:GSE
X 框架选项:SAMEORIGIN
内容类型:application/json
缓存控制:无缓存、无存储、max-age=0、必须重新验证
传输编码:分块
替代协议:443:quic
X-XSS-保护:1;模式=块
到期时间: 1990 年 1 月 1 日星期五 00:00:00 GMT