Yoan Blanc’s weblog

Another lost swiss guy

Posting on a Page from a Facebook Application

Yoan BlancMon, 31 Jan 2011, , ,

No revolutions here but since Facebook’s documentation always lacks a bit behind because they move too fast, it might be useful to some of you.

First things to know is that pages are owned by users not applications and you can only interact with facebook as an application on the behalf of a user. First thing first, you need to create an application.

To act on a Page from an application, the administrator of a Page your application want to administrate must give you the permission to manage his pages. This permission is called, no surprises here: manage_pages.

Once the permission is granted, an access_token can be retrieved from https://graph.facebook.com/me/accounts and used from the application to interact with the page.

Nowadays Facebook applications should be made using mostly the JavaScript SDK to login, logout and so forth. That said, ours gonna be static only as we don’t need more.

Okay, now you have an application and a page with its access_token. Let’s have fun with the Graph API. Frankly, there isn’t much I can say against the Graph API. It’s a nice example of REST API build upon a very complex data schema.

Let’s submit something.

<?php
$fb = new Facebook(array(
 appId => null,
 secret => null
));
$message = 'Hello World!';
$access_token = /* token for the page */;
$fb->api(
 '/pageNameOrId/feed',
 'POST',
 compact('access_token', 'message')
);

This a simple message, you can make it way more complex. The message will appear as being posted by your application, so pick a cool name.

This is it.

Aujourd’hui pas de folie, un simple écrit sur un truc pas assez, ou assez mal, documenté par Facebook lui-même et qui pourra être utile à certains. Comment poster un message, lien, … via un script sur le mur (wall) d’une page ?

La réponse courte, il faut le faire par le biais d’une application dont un administrateur de la page aura préalablement autorisé la dite application de la gérer.

Donc pour commencer, créez une application, cette application sera la personne qui postera sur la page en question.

L’évolution de Facebook a fait qu’aujourd’hui il est recommandé d’éviter FBML (des tags XHTML) au profit d’XFBML qui est, grosso-modo la même chose, mais interpréter côté client par leur bibliothèque JavaScript. Donc, une iframe et une page statique suffiront à notre expérience.

Ensuite le but est de se connecter à l’application, et l’autoriser à gérer vos pages via la permission : manage_pages, ça ne s’invente pas. Une fois autorisée, l’application peut demander à Facebook des jetons d’authentification (OAuth 2.0) par pages autorisées. Ceci fait, il est temps de s’amuser.

import facebook

access_token = 'access token'
obj = {'message': 'Hello!'}

fb = facebook.GraphAPI(access_token)
fb.put_object('pageNameOrId', 'feed', **obj)

Et voilà, c’était plus simple que prévu, non ? Un gist contient les codes sources.

About

meYoan Blanc is a web developer that lives in Switzerland (rue des Fahys 15, 2000 Neuchâtel) and works as a freelance.

Get my vCard or contact me by phone (skype:yoan.blanc) or email ().

Misc

RSS, list.blogug.ch

This site reflects only my opinion and is not affiliated with anyone else.

copyright 2006-2010 — doSimple.ch