Last Updated: February 25, 2016
·
2.51K
· onehouse

Using Facebook's API to post to my organization's FB page from my organization's website

Implementing this use case with the Facebook API seemed oddly difficult/complicated to me, so I figured I'd document it.

I have a small website for an organization. It's a sort of blog-like CMS site, built with Rails. When content gets published on the site, I wanted to post a status message about the new content on the organization's Facebook page.

Here are the steps I went through to get a non-expiring page access token that can be used to post to a Facebook page:

Step 1: Create Facebook App

  1. navigate to: https://developers.facebook.com/
  2. click Apps > Add New App
  3. select the "www" option
  4. enter some info about your app, then skip the quick start (it seems unnecessary)
  5. obtain App ID and App Secret for the app

Step 2: Get Short-lived User Token Associated With App

  1. on developers.facebook.com, click Tools > Graph API Explorer
  2. select your new Facebook app's name from the application select box
  3. click the Get Access Token button
  4. select 'extended permissions' link, check 'managepages' and 'publishactions', and submit
  5. retrieve the User Access Token that has been added to the access token field

Step 3: Get Longer Lived User Token

The previous token will expire in a couple of hours. Facebook will let you get a token that will last for 60 days.

You can use the Facebook Graph API Explorer for this, but I chose to use curl on the command line:

curl -X GET "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=[your facebook app id]&client_secret=[your facebook app secret]&fb_exchange_token=[short lived user access token from previous step]"

the output will look something like, containing the Extended Life User Token:

access_token=[the new longer life user token]&expires=5184000

Step 4: Get Page Access Token

In order to post to the status timeline for the page, I need to post as the page. To do this, I need to use my new longer life user token to get the page token.

Using curl again:

curl -X GET "https://graph.facebook.com/v2.2/me/accounts?access_token=[the new longer life user token]"

This will result in something like, the following, which contains the Page Access Token:

{
    "data": [
        {
            "access_token": "[new page access token]",
            "category": "Non-profit organization",
            "id": "mypageid",
            "name": "Mypagename",
            "perms": [
                "ADMINISTER",
                "EDIT_PROFILE",
                "CREATE_CONTENT",
                "MODERATE_CONTENT",
                "CREATE_ADS",
                "BASIC_ADMIN"
            ]
        }
    ], 

When I debug the page access token at https://developers.facebook.com/tools/debug/, it tells me that the token never expires.

Step 5: Post Status Message to Page

Again with curl:

curl -X POST https://graph.facebook.com/v2.2/mypageid/feed -F 'access_token=[page access token]' -F 'message=posting with page token'