invalid oauth2 token request

google-api, node.js, oauth, oauth-2.0

Solution

It turns out that request.js (https://github.com/mikeal/request) doesn't automatically include the content-length to the headers. I added it manually and it worked on the first try. I've pasted the code below:

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();
                }
            }
        }
    );
}

Problem

I'm developing a node application which needs to authenticate with google. When I request a token, https://accounts.google.com/o/oauth2/token responds with: ``` error: 400 { "error" : "invalid_request" } ``` I've tried making the same request in curl, and have received the same error, so I suspect there is something wrong with my request but I can't figure out what. I've pasted my code below: ``` 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); } }); ``` I've triple checked to make sure all the data I'm submitting is correct and i'm still getting the same error. What can I do to debug this further?

Original source

Related problems