Search code examples
node.jsfacebook-graph-apipermissionsfacebook-pagefacebook-access-token

Posting a Post through Facebook Graph Node JS


I'm currently looking to automate a few tasks by creating a custom application that allows me to easily post status updates on my Facebook page(not my personal timeline). My backend is running on NodeJS.

My client-side code has the following:

window.fbAsyncInit = function() {
FB.init({
    appId : 'xxxxxxxxxxxxx', // App ID
    channelUrl : '//xxxxxxxxxxx, // Channel File
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml : true // parse XFBML
});

// Additional init code here
FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        console.log("You are signed into FB");
        var access_token = FB.getAuthResponse()['accessToken'];
        console.log('Access Token = ' + access_token);
        FB.api('/me/accounts', function(response) {
          for(var i=0;i <response.data.length;i++){;
            var page = response.data[i];
            if(page.id==my_id){
                var page_token = page.access_token;
                console.log(page_token);
                var param = 'callback=?&username=' + GetURLParameter('username')+'&session='+ GetURLParameter('session')+'&access_token='+ page_token;
                $.post(saveTokenEndpoint, param, function(data) {
                    console.log(data);
                });
            }
          }
        });

        FB.api('/me', function(response) {
            username = response.name;
            userid = response.id;
            $('#username').text('~Hi '+username+'!');       
            $(document).trigger('fbInit');
        });

        // connected
    } else if (response.status === 'not_authorized') {
        // not_authorized
        login();
    } else {
        login();
        // not_logged_in
    }

});

};

// Load the SDK Asynchronously
( function(d) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));

function login() {
FB.login(function(response) {
    if (response.authResponse) {
        console.log("You are signed into FB");
        var access_token = FB.getAuthResponse()['accessToken'];
        FB.api('/me', function(response) {
            username = response.name;
            userid = response.id;
            $('#username').text('~Hi '+username+'!');
            $(document).trigger('fbInit');
        });
        FB.api('/me/accounts', function(response) {
          // handle response
          for(page in response.data){
            if(page.id==my_id){
                var page_token = page.access_token;
                console.log(page_token);
                var param = 'callback=?&username=' + GetURLParameter('username')+'&session='+ GetURLParameter('session')+'&access_token='+ page_token;
                $.post(saveTokenEndpoint, param, function(data) {
                    console.log(data);
                });
            }
          }
        });

    } else {
        // cancelled
    }
}, {scope: 'publish_stream, manage_pages, offline_access'});
}

My server-side code:

// saves the token received for future use in posting
app.post('/api/saveToken',function(req,res){
    var username = req.body.username;
    var access_token = req.body.access_token;
    var session = req.body.session;
    user.findOne({
        username: username,
        session: session
    },function(err, userObj){
        if(userObj!=null){
            userObj.token = access_token;
            console.log(access_token);
            userObj.save();
            res.jsonp({status:'true'});
        }else{
            res.jsonp({status:'false'});
        }
    });
});

I'm using this method to post my posts on my page:

var data = querystring.stringify({
access_token: token,
message: message
});

                        var options = {
                            host: 'graph.facebook.com',
                            port: 443,
                        path: '/app_name/feed',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'Content-Length': data.length
                        }
                    };

                    var req = https.request(options, function(res) {
                        res.setEncoding('utf8');
                        res.on('data', function (chunk) {
                            console.log("body: " + chunk);
                        });
                        res.on('end', function(){ // see http nodejs documentation to see end
                            console.log("\nfinished posting message");
                            conObj.approval = 'published';
                            conObj.save();
                        });
                    });
                    req.on('error', function(e) {
                        console.error(e);
                    });
                    req.write(data);
                    req.end();

Current situation:

My code is working and i can actually see the posts on the wall of my page.

BUT

It does not appear to anyone else except myself.

HOW I GOT IT TO APPEAR PROPERLY TEMPORARILY: When i copy the token directly from the Graph API Explorer(/me/accounts), I got it to work. https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts

Ultimately, all i want to do is find a way to post a post on my page

I monitored the differences that was made through both tokens and have found no differences. Their scope was to the 'public' and 'everyone'.

Experts do advice! I find this all very puzzling.

I'm intending to try NodeJS facebook such as facebook-node-sdk and nodejs-facebook-sdk libraries if all else fails.

Thanks for the replies in advance!


Solution

  • You should do OAUTH server side, not client side.

    Here is a working example of posting a post through the Facebook Graph API using node.js here: http://code.runnable.com/facebook/UTlPM1-f2W1TAABY

    The only dependency you're not already using is request, which you should be using.