Question

I'm trying to set up a Facebook login flow in my node.js app but for some reason I cant get the access token to return from the Facebook API when I use node's https.get(). I can get the access token when I use curl though, so I'm not sure what node is doing differently. Relevant code:

var express = require("express");
var https = require("https");

var app = express();

app.route("/")
    .all(function(req, res, next)
    {
        res.sendfile("index.html");
    });

app.route("/login")
    .get(function(req, res, next)
    {
        res.redirect("https://www.facebook.com/dialog/oauth?" +
            "client_id={my_client_id}&" +
            "redirect_uri=http://localhost:3000/auth");
    });

app.route("/auth")
    .get(function(req, res, next)
    {
        var code = req.query.code;
        https.get("https://graph.facebook.com/oauth/access_token?" + 
            "client_id={my_client_id}" +
            "&redirect_uri=http://localhost:3000/auth" +
            "&client_secret={my_client_secret}" +
            "&code=" + code,
            function(token_response)
            {
                // token_response doesn't contain token...
                res.sendfile("logged_in.html");
            }
        ).on("error", function(e) {
            console.log("error: " + e.message);
        });
    });

var server = app.listen("3000", function()
{
    console.log("listening on port %d...", server.address().port);
});

token_response ends up being a huge object that seems to have nothing relevant to the access token. The Facebook developer docs say I'm supposed to get back: access_token={access-token}&expires={seconds-til-expiration} which is exactly what I get when I use curl, but not node.

Was it helpful?

Solution

A bit late. But did you ever solve this?

It seems that you were incorrectly handling the HTTPS response.

http://nodejs.org/api/https.html

https.get("https://graph.facebook.com/oauth/access_token?" + 
        "client_id={my_client_id}" +
        "&redirect_uri=http://localhost:3000/auth" +
        "&client_secret={my_client_secret}" +
        "&code=" + code,
        function(res)
        {
            res.on('data', function(chunk) {
                console.log(chunk);
            });
        }
    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top