Friday, September 30, 2011

Posting to a facebook page as an admin AKA WHY IS THIS SO DIFFICULT!?

Tonight I have wasted the better part of 6 hours trying to figure out how to post to a facebook page I administer. Just thinking about that, it seems incredibly easy, after all, I wrote a twitter api class in about 30 minutes that sends out tweets. This should be cake.

It is not.

I had never delved too deeply into the facebook developers API, oddly enough called the "graph" api (don't understand that one Zucks). Though deeply you must delve in order to understand the convoluted and flat out missing parts of these docs. What should take an hour or two tops is complicated by navigating the insane twists and turns of this documentation. However I did ultimately surface and figure out what I needed to do which I will now share with you.

Step 1) Create a new application.

Really the only reason we do this is to get the app id and secret I wouldn't worry too much about the details here this thing is merely a face to our api calls. You most likely wont be promoting it too much if at all.

Step 2) Add your info to this link.,publish_stream,offline_access&response_type=token

YOUR_APP_ID is obviously the id of the app you just created and YOUR_URL should be the url your app is associated with (urlencoded of course). Finally the thing that was missing from all the documentation and what I had to piece together is the scope parameter. Notice how I added the publish_stream portion to it which is missing from any references to the "Authentication" for pages. Why is it missing? Because we are authenticating the application with our account NOT the pages. Yep you heard right in order to write to a PAGE we have to authenticate an app for our personal facebook account. The offline_access value will make a long lived access token so you shouldn't have to worry about authenticating again.

Step 3) Add your info to this link.

After you accept the application for your own account you will be redirected back to the url you specified with a shiny new access_token. Place the access token in the TOKEN_FROM_ABOVE position and make sure you do it quickly those things don't last long in OAuth 2.0.

Step 4) Copy what you need.

The link above should return a bunch of JSON showing all the pages you administer with access_tokens for each. I suggest you copy them all because they don't expire and you wont have to go through this crap again if you want to add the same functionality to another site you admin.

Step 5) Download the facebook SDK and add your stuff

Once you have downloaded the SDK (alter my code to fit your language) then copy your info as follows:

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',

  // Proceed knowing you have a logged in user who's authenticated.
  $fields = array (
    'message' => 'Hello World',
    'access_token' => urlencode('YOUR_PAGE_ACCESS_TOKEN')

  $facebook->api('/me/feed', 'post', $fields);
catch(FacebookApiException $e)
  $user = null;

There are a few things to note here. The first is that you want to make sure you copy the page access token which is what you got from all that JSON in the previous step. The second is that you want the uri to be '/me/feed' because you are authenticating as the page.

Follow those steps and you should be able to post to your facebook pages without any trouble, at least until they decide to change something like they always do.