Posting with the Facebook api via Node.js

— 3 minute read

In yesterday's post, we started our RSS social posting app by reading the RSS feed from the website.

Today we are going to post to Facebook.

Read more: Basic Node.js Express application

node.js post to Facebook permalink

Facebook has quite an extended sign up process, so be aware it might take some time to get an actual developer account.

Visit: Facebook developers to sign up for a developer account

Once you have your Facebook developer account, start by creating a new app.

Next, it's easiest to open the Facebook Graph API Explorer which you can find here

Change the URL to be DailyDevTipsBlog/feed obviously change DailyDevTipsBlog to your page.

We need to add permissions for:

manage_pages
publish_pages
publish_to_groups

And add the message in the left column as following JSON

{ 
		"message": "Testing with api" 
}

We have to get a new Access token at this point, click the button and it will ask you if it's ok to use these new permissions.

If we now click Submit, we should get a response with an id this means the post is now on your page!

Success!! 👏

Connecting to Facebook API via node.js permalink

Obviously, Facebook helped us out a lot, and I know you're scared, but in node.js, it's easier!

First, we go back to our rss-app directory in the terminal and execute the following command:

npm install fb

This will install this package.

Now we add our code to do the actually posting.

var FB = require('fb');

FB.setAccessToken('ACCESS_TOKEN');
FB.api(
'/DailyDevTipsBlog/feed',
'POST',
{ "message": "Testing with api" },
function (response) {
console.log(response);
}
);

In the first line, we define that we want to use the Facebook package. Then we set the access token (we can get that from the graph API for now!) Then, we call the API and do the same as we did in the graph API!

Easy as that! 🙄

Stay tuned and connected! permalink

Find today's code here on GitHub

In the meantime, feel free to send me a message on Facebook or Twitter and keep an eye out for tomorrow's Twitter integration!