(Nodejs)Google oAuth2:缺少必需参数:grant_type

时间:2014-05-12 12:33:09

标签: node.js oauth-2.0 request google-api

使用Nodejs和Mikeal的请求进行此调用。做文档所说的所有事情here,包括grant_type = authorization_code,但我仍然会遇到同样的错误。请参阅截图,了解我传递的确切变量和方法。

Screenshot of error

以下是使用grant_type显式设置第一个

的路径
/o/oauth2/token?grant_type=authorization_code&code=4%2F42sz3xxq4wGF9k9joYxRVop9mHi6.UpFaIUqZ_UQaaDn_6y0ZQNh869DgiwI&client_id=199079322117.apps.googleusercontent.com&client_secret=...&redirect_uri=...

2 个答案:

答案 0 :(得分:1)

在交换令牌时,需要在初始oauth请求中发送重定向uri,以便与之匹配。

在这里发表我的答案,解释我是如何使用弹出窗口构建自己的oauth用户流程(而不是在主应用程序中重定向用户)Node Google OAuth2 redirect_uri_mismatch

答案 1 :(得分:0)

Yeah I know its a very old question but I faced the same problem today.
and resolved the  issue as follow. 
if you use form option it will automatically set content type
appticationx-ww-orm-urtencoded



  var request = require('request');
  request.post('https://accounts.google.com/o/oauth2/token',
      {
          form: {
              code: authorization code, // which you get from google 
              client_secret: 'client secret',// its your app secret
              client_id: 'client id', //app client id
              grant_type: 'authorization_code',
              redirect_uri: 'https://developers.google.com/oauthplayground' //this should match the redirect uri which you give while creating the client id
          }
     },function(err,res,body){
         console.log(body);
         if(err) console.log(err);
         //console.log(res);
     });