Google OAuth2:缺少必需的参数:grant_type [英] Google OAuth2: Required parameter is missing: grant_type

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

问题描述

我已经尝试了几乎所有的方法,阅读了关于这个问题的所有 StackOverflow 帖子,但我仍然无法让它工作.有趣的是,通过 DHC REST API 客户端(Google Chrome 应用程序)发送 POST 请求时,我能够获得 200 OK 响应.

I have tried just about everything, read every StackOverflow post on this issue but I still can't get it to work. Interestingly enough, I am able to get 200 OK response when sending a POST request via DHC REST API Client (Google Chrome app).

  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 请求发送.令人惊讶的是,在试图弄清楚一个多小时后,如此微不足道的事情却被忽视了.

As @BenFortune has already mentioned, I was sending GET parameters as a POST request. It's amazing such a trivial thing has gone unnoticed after trying to figure it out for over an hour.

现在,我将此归咎于 OAuth 提供商之间的不一致.在同一个应用程序中,我正在向 Facebook 发出 GET 请求以获取 access_token:https://graph.facebook.com/oauth/access_token.但是 Google 需要 POST 请求来获取 access_token:https://accounts.google.com/o/oauth2/token

Now, I blame inconsistencies across OAuth providers for this. In the same application I am doing a GET request to Facebook to obtain access_token: https://graph.facebook.com/oauth/access_token. But Google expects a POST request to obtain 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);
  });

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

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