【问题标题】:Google OAuth2: Required parameter is missing: grant_typeGoogle OAuth2:缺少必需参数:grant_type
【发布时间】:2014-07-30 06:43:45
【问题描述】:

我几乎尝试了所有方法,阅读了有关此问题的所有 StackOverflow 帖子,但我仍然无法使其正常工作。有趣的是,当通过 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);
  });

【问题讨论】:

  • 您将请求作为 POST 发送,但参数作为 GET。
  • @BenFortune /faceplam。我知道我在做一些愚蠢的事情。感谢您发现我的错误。

标签: node.js authentication oauth-2.0 google-oauth


【解决方案1】:

正如@BenFortune 已经提到的,我将GET 参数作为POST 请求发送。想了一个多小时,竟然没有注意到这么一件微不足道的小事。

现在,我将此归咎于 OAuth 提供商之间的不一致。在同一个应用程序中,我正在向 Facebook 发出 GET 请求以获取 access_token:https://graph.facebook.com/oauth/access_token。但是 Google 期望通过POST 请求获得access_tokenhttps://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);
  });

【讨论】:

  • 嗨,我遇到了同样的错误。我尝试了您所做的,但收到此错误:XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 400.
  • omg,fu*ing 邮递员,我插入的是 url 参数而不是 POST 参数...
【解决方案2】:

检查请求编码。

在我的情况下,我发送的是 .json 并且是 .url

使用 Alamofire 3.0

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2019-12-11
    • 2014-09-04
    • 2012-01-02
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多