【问题标题】:invalid oauth2 token request无效的 oauth2 令牌请求
【发布时间】:2012-06-08 07:10:08
【问题描述】:

我正在开发一个需要通过 google 进行身份验证的节点应用程序。当我请求令牌时,https://accounts.google.com/o/oauth2/token 会回复:

error: 400
{ 
  "error" : "invalid_request"
}

我尝试在 curl 中发出相同的请求,并收到相同的错误,所以我怀疑我的请求有问题,但我不知道是什么问题。我在下面粘贴了我的代码:

var request = require('request');
var token_request='code='+req['query']['code']+
                  '&client_id={client id}'+
                  '&client_secret={client secret}'+
                  '&redirect_uri=http%3A%2F%2Fmassiveboom.com:3000'+
                  '&grant_type=authorization_code';
request(
    { method: 'POST',
      uri:'https://accounts.google.com/o/oauth2/token',
      body: token_request
    },
    function (error, response, body) {
        if(response.statusCode == 201){
            console.log('document fetched');
            console.log(body);
        } else {
            console.log('error: '+ response.statusCode);
            console.log(body);
        }
    });

我进行了三次检查,以确保我提交的所有数据都是正确的,但我仍然遇到同样的错误。我该怎么做才能进一步调试?

【问题讨论】:

  • 在这里发布从 token_request var.that 生成的最终字符串可能有问题。

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


【解决方案1】:

事实证明,request.js (https://github.com/mikeal/request) 不会自动将内容长度包含在标头中。我手动添加了它,它在第一次尝试时就起作用了。我已经粘贴了下面的代码:

exports.get_token = function(req,success,fail){
    var token;
    var request = require('request');
    var credentials = require('../config/credentials');
    var google_credentials=credentials.fetch('google');
    var token_request='code='+req['query']['code']+
        '&client_id='+google_credentials['client_id']+
        '&client_secret='+google_credentials['client_secret']+
        '&redirect_uri=http%3A%2F%2Fmyurl.com:3000%2Fauth'+
        '&grant_type=authorization_code';
    var request_length = token_request.length;
    console.log("requesting: "+token_request);
    request(
        { method: 'POST',
          headers: {'Content-length': request_length, 'Content-type':'application/x-www-form-urlencoded'},
          uri:'https://accounts.google.com/o/oauth2/token',
          body: token_request
        },
        function (error, response, body) {
            if(response.statusCode == 200){
                console.log('document fetched');
                token=body['access_token'];
                store_token(body);
                if(success){
                    success(token);
                }
            }
            else {
                console.log('error: '+ response.statusCode);
                console.log(body)
                if(fail){
                    fail();
                }
            }
        }
    );
}

【讨论】:

  • 那是在HTTP POST request in node.js 示例中作为第二个标题:)
  • POST 示例不起作用,因为它没有发送 google 需要的所有标头。
【解决方案2】:

从这里How to make an HTTP POST request in node.js? 你可以使用querystring.stringify 来转义请求参数的查询字符串。另外,您最好为 POST 请求添加'Content-Type': 'application/x-www-form-urlencoded'

【讨论】:

  • 我尝试使用 require 将其作为表单提交以将其添加为默认内容类型,但仍然失败。
【解决方案3】:

在此处发布从 token_request var.that 生成的最终字符串可能有问题。或者可能是身份验证代码已过期或未正确添加到 URL。通常代码中有'/'需要转义。

【讨论】:

    最近更新 更多