How to publish actions on facebook timeline (for facebook apps)

Update: This article was written in 2011 and not applicable now. We have another article published in 2015 that talks about how to publish stories/actions on facebook timeline

timeline-psd

You would have noticed updates like

Somefriend has won the battle game
Somefriend has read this article in this app

These are called activities and they are posted from facebook applications, when you sign up for an app you will have to allow the app access some details and perform some actions on your behalf with your facebook account.

As a developer you may be interested in knowing how these activities are published and how to use it on your app.  To get these activities published on app users’ timeline, they should have granted a permission(a.k.a as scope) called publish_actions. This permission will also let the app do timeline stream publishing on behalf of the users.

Facebook tries to protect the privacy of the user,  and disallows  some of the actions performed using this permission.  User should always know the outcome of their action they do inside the app.

Let us get on to step by step implementation of publish actions. There are some entities or jargons that should be used to define the steps.

1. Action types – action performed by user. It could be anything, facebook takes effort to change the tenses of the action word that you are creating as an action type

2. Object types – is an entity on which the action was performed.

ex:  X wrote an article

wrote(write) is an action type and article is an object type.

Following steps can be done in your developer section, Goto -> Edit app and you will find the open graph link on the left.

Create object types

First object should be created to connect with action that user can perform with your app. I am taking our launcher app for example, with which users can create page tabs for facebook pages. So here page tabs is the object type.

Create action types

There can be many action types, now an action type has to be specified with connection to the object that was created. Taking the same example, User can create page tabs CREATE is the action type that needs to be created.

 

 

 

Get object type code

After creating object types and action types, click on get code near the object type and copy the code that it shows up in the popup to your app page (iframe or website app).

Publishing sample action type on timeline

Now sample action has to be published. Why? see the next step.

You may publish the action type using php

curl -F 'access_token=actual user access toke' \ -F 'page_tabs=page url where object code was pasted' \ 'https://graph.facebook.com/me/namespace:action'

or with JS SDK

FB.getLoginStatus(function(response) {
   if (response.status === 'connected') {
     var uid = response.authResponse.userID;
     var accessToken = response.authResponse.accessToken;
     FB.api('/me/namespace:action?access_token='+accessToken+'', 'post', { object : 'url' }, function(response){
          if(response.id)
           console.log(response.id+ " Response" );
     });
   }else if (response.status === 'not_authorized') {
         // the user is logged in to Facebook, 
         // but has not authenticated your app
   }else {
        // the user isn't logged in to Facebook.
   }
});

This can publish sample action to your wall

Applying for approval

To publish these actions, developers must get approval from facebook for the action types, for which action type has to be published once by the developer.

You must explain how to publish this action while applying for approval. Once the action type is approved it will start appearing on app users’ timeline when they do that action in your app.

Common errors

Action should not happen automatically or on sign up and should happen only when user performs some operation in the apps.

Error code 3502 and 206 are common when the page url mentioned in the action publish code has a redirect or wrong data, so to ensure that the data pasted in the page is properly read by facebook you may use debugger tool. This may also be due to access token.

Some useful links to read before starting

These links talks about policies to follow while creating action types,

https://developers.facebook.com/docs/opengraph/guidelines/#uex

Related

facebook_sdk_update2