Google OAuth2:缺less必需的参数:grant_type
我已经尝试了一切,阅读每个StackOverflowpost在这个问题上,但我仍然无法得到它的工作。 有趣的是,通过DHC REST API客户端(Google Chrome应用程序)发送POST请求时,我能够获得200 OK响应。
var url = 'https://accounts.google.com/o/oauth2/token'; var params = querystring.stringify({ grant_type: 'authorization_code', code: req.body.code, client_id: req.body.clientId, client_secret: 'HIDDEN', redirect_uri: req.body.redirectUri }); params = querystring.unescape(params); // doesn't work with or without string escaping request.post(url + '?' + params, function(error, response, body) { console.log(body); });
由于@BenFortune已经提到,我正在发送GET
参数作为POST
请求。 试图找出一个小时以后,这样一件小事已经被忽视了,真是太神奇了。
现在,我将这个问题归咎于OAuth提供商的不一致之处。 在同一个应用程序中,我正在对Facebook进行GET
请求以获取access_token
: https : //graph.facebook.com/oauth/access_token 。 但Google希望通过POST
请求获取access_token
: https : //accounts.google.com/o/oauth2/token
正确的版本:
var url = 'https://accounts.google.com/o/oauth2/token'; var payload = { grant_type: 'authorization_code', code: req.body.code, client_id: req.body.clientId, client_secret: 'HIDDEN', redirect_uri: req.body.redirectUri }; request.post(url, { form: payload }, function(error, response, body) { console.log(body); });
检查请求编码。
在我的情况下,我发送.json和.url
使用Alamofire 3.0