test.ical.ly | getting the web by the balls

Sep/11

27

5 steps to automatically write on your Facebook Page wall using the Graph API without a logged-in user

Yesterday I spend quite some time only to figure out how to access the Facebook Graph API without depending on the current user being logged in. Basically I want to be able to i.e. post a status message from my Facebook app to the wall of one of my pages without user credentials.

It’s not rocket science. :)

Like I said it’s not hard but it took me a while to find out how to do it. Finally I stumbled upon a blogpost called Permanent Facebook Sessions and Posting as a Page.

The problem I had to solve was that the Facebook Graph API always needs an access token to do anything (except for a few bits) which is usually obtained from the currently logged in user.

However this access token doesn’t last very long and when I want to update my pages wall I want to do that with a cronjob so there is no user to obtain one from.

And a user should not be necessary anyway as it is my Facebook Application posting to my Facebook Page.

So in a nutshell the following steps are required.

1. Create your Facebook Page

If you haven’t done so. Note down its ID.

2. Create your Facebook Application

Most important thing to do is to configure your app to be integrated on a Website.

Also note your applications ID and secret.

3. Authorize your application to manage your page offline

Browse to the following URL.

https://graph.facebook.com/oauth/authorize?client_id=[APP_ID]&scope=offline_access,publish_stream,manage_pages&redirect_uri=[YOUR_SITE]

 

Replace [APP_ID] with your applications ID and [YOUR_SITE] with the urlencoded Site URL you configured in step 2.

You will be asked to grant the requested permissions to your app.

You will see the name of your Page listed below Manage my Pages.

After allowing you will be redirected to the website you configured. You will notice that the URL of your website now contains an additional parameter code with a value of about 160 characters.

http://your_site/?code=AQB4gANRVTyvTEz5C..

4. Get your offline access token

Now browse to the following URL.

https://graph.facebook.com/oauth/access_token?client_id=[APP_ID]&redirect_uri=[YOUR_SITE]&client_secret=[APP_SECRET]&code=[THE_CODE]

Again replace [APP_ID] with your applications ID and [YOUR_SITE] with the urlencoded Site URL you configured in step 2. Also replace [APP_SECRET] with your applications secret and [THE_CODE] with the value of the code parameter that was passed to your website in step 3.

In your browser you will now see a string like this:

access_token=LHgHGjlhiugHKhhgkjjkhzt676ziuhztZUGUZFRtjhhjkf66tu6ziuzghJKHGGFDSkghjgHHGGHgfzFHJggfHGFgggfHG7rt78zghjghjg

Update!

You now have a permanent token for your own account. Whatever you will do with it will be done as yourself. Please read my follow-up if you want to act as your Page itself before you continue with the next step..

5. Post a status to your Facebook Page wall

For the convenience of this post I opened up the Facebook Graph API explorer for this.

Just enter the access token you received from step 4 and POST a message to your Facebook Pages feed.

https://graph.facebook.com/[PAGE_ID]/feed

 

The access token is valid for about one year from the last action done with it. So if the time between two of your posts will always be less than one year your token will never expire.

·



<<

>>

Theme Design by devolux.nh2.me