How To Publish Actions On Timeline

By | November 11, 2011

This is my second tutorial of Facebook application development with Open Graph series, in previous tutorial I showed you how to setup your Facebook application with Open Graph. Now in this tutorial I will teach you to how to publish actions on user’s timeline.

Note: Due to changes announced by Facebook on 31st May this tutorial has been updated you can visit the updated tutorial from here.

How to Publish Actions on User’s Timeline
If you have already created actions and objects by following my previous tutorial then the next setup is to publish those actions on user’s timeline, but before proceeding you can check my demo app.

Step 1: Go to developers.facebook.com/apps select the relevant app if you have multiple apps and click on Edit open Graph in Open Graph section.

Step 2: Click on Get Code in Object Types and copy the highlighted code from popup window and save it somewhere.

Step 3: Click on Get Code in Action Types and copy access token as shown in the second image and save it also.

Step 4: Now create a file test.html, paste the following code in it, replace the colored text according to your app’s settings and upload it to your hosting server yoursite.com/test.html.

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"
xmlns:fb="https://www.facebook.com/2008/fbml">

Paste the code copied in step 2 here and don’t forget to replace Put Your Own URL Here with yoursite.com/test.html in og:url, set descriptions and image according to you page content.

<script type="text/javascript">
function read()
{
FB.api(‘/me/Your_Namespace:action‘ +
‘?object=Yoursite.com/test.html&access_token=Paste the access token copied in step 3‘,’post’,
function(response) {
var msg = ‘Error occured’;
if (!response || response.error) {
if (response.error) {
msg += "\n\nType: "+response.error.type+"\n\nMessage: "+response.error.message;
}
alert(msg);
}
else {
alert(‘Post was successful! Action ID: ‘ + response.id);
}
});
}
</script>

</head>

<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:’Your_appId‘, cookie:true,
status:true, xfbml:true, oauth:true
});
</script>
<form>
<input type="button" value="Post To Your Timeline " onclick="read()" />
</form>
</body>
</html>

Your_Namespce: In my demo app it was onlytipsandtricks change it according to your app.

Action: In demo app it was read but when you click on get code as you did in step 3 you will see underscore after read as read_

You have to include these underscores in both actions and objects otherwise you will get the following error:

The action you’re trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: article_.

Object: We used object article in demo app so you have to replace it according to object settings of your app.

Your_appId: You application id.

Finally you can test your page by visiting yoursite.com/test.html and clicking on Post To Your Timeline button, if all things go well you will see Post was successful! message along with app id on your page.


Related Posts:



Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

52 thoughts on “How To Publish Actions On Timeline

  1. Andrei

    When I click “Publish to Timeline” button I get an error “session is expaired”.

    Reply
  2. Patel pranay

    when I click on “publish to timeline”, nothing happens! :( Help soon if possible

    Reply
  3. peeyush

    The action you’re trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: article_.

    I am getting the above error as per your doc. i have made chages but not working plz tell wat i do for it .

    Reply
    1. Hasnain Post author

      Have you added underscore “_” after “article_” in this line FB.api('/me/Your_Namespace:read_' +
      '?article_=Yoursite.com/test.html&access_token=Paste the access token copied in step 3','post',

      Reply
        1. Hasnain Post author

          I have checked your page on Debugger its ok, if you added the same objects and actions types in your app and still you are getting this error then may be its a Facebook bug many people are complaining this, here is same question from stackoverflow although its related to submitting the object.

          Reply
  4. Akansh

    Hello!i loved ur article and want to know is it possible that when any person visit my blog and read any article ,when he reads it get posted on facebook that That person has read an article..like today big sites have this feature.please help/

    Reply
    1. Hasnain Post author

      Yes its possible but first thing you need is the user visiting your site for reading articles must authenticate your app before read action can be posted to his timeline, you can use “add to timeline plugin” for this purpose. The other thing is your pages must have open graph meta tags matching with your app’s actions and objects, if you have self hosted wordpress blog there are few plugins with can add meta tags to your pages.

      Reply
      1. Akansh

        But i am using blogger and it`s really difficult to start that service.Can You write a tutorial for enabling that sevice for a blog.My blog is paisaearn.in

        Reply
        1. Jano

          Hello Hasnain:

          I want exactly what Akansh described.

          I am already using a plugin for OGP meta tags that is doing its job very well. But I can not find an “Add to timeline plugin”. Would you point me in the right direction? Thanks a lot.

          Reply
  5. Janusz Jasinski

    I keep getting this:

    Message: The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: article.

    I’ve followed this article down to the last full stop but no luck.

    Any ideas?

    TIA

    Reply
    1. Janusz Jasinski

      Ok – I clearly didn’t see everything! Brilliant – thanks.

      Someone did mention a good example of something I’m look to do

      1) If app not authenticated
      1.1) Show “Add to Timeline” or “Authenticate this App” button
      2) If app is authenticated
      2.1) Run the function to add the “read an article” to the timeline
      2.2) If they come back to the page, check if the actio has already been submitted
      2.3) If submitted already i.e. already seen article, don’t add again

      Reply
      1. Hasnain Post author

        1. You can add the “Add to timeline” plugin, by using this plugin if the user is not authenticated he will be shown “Add to timeline button” and if the user already added the app then “added to timeline” will be shown.
        2. If user is authenticated with publish_actions permission then you can publish “read” actions by allowing user to click on the custom button as we did in the example or you can call javascript function by using this bit of codeonload="read() so that when the user clicks on link to article the action will be posted automatically when the page will be loaded.

        Reply
  6. AkRy

    HI !
    I’ve followed your tutorial step by step and it’s still not working…
    I’ve got my app on my application page and i see
    Add to timeline when i click on it nothing happenned to my timeline :(
    I have also a message error which is
    Message(#3502) Object at URl “my URL” has og:type of’website’
    The property ‘article’ requires an object of og:type ‘testserd:article’
    **testserd is my app namespace

    Do you know how to fix this?

    Reply
          1. Avinash Agrawal

            Yes… Now it is showing error….

            The action you’re trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: article.

        1. Avinash Agrawal

          I am getting this in Action type

          FB.api(‘me/news.reads’ …….

          Instead of

          FB.api(‘/me/Your_Namespace:action….

          Semicolon is missing ???

          Reply
  7. Avinash Agrawal

    Erro code(#100) Can only call this method on valid test users for your app”

    but after sometime
    error: session has expired at unix time……

    What is this?? plz help me out

    Reply
    1. Hasnain Post author

      “Session expired” means the access token has expired the token usually expires within two hours, now you have to copy the the token again as mentioned in step 3.

      Reply
  8. JC

    i tried everything but nope, nothing happens when i click “post to my timeline”. why do you think i am even not getting an error message? thanks in advance

    Reply
    1. Hasnain Post author

      First make sure that all the code is in plain text, if you have copied the code from here then chances are that you have copied the formatted text and you have to convert it into plain text, if the code you have copied is already in plain text then check if you have correctly loaded the SDK and additionally you can check the source code of my app.

      Reply
    1. Hasnain Post author

      I am out of thoughts because the link you have posted is not working anyways i guess it is itsoutsideinside.com, i have checked your pages source code its showing strange characters.

      Reply
  9. cateno

    I followed the example above but i keep receiving tha same erron when i click in “publish” button:

    Message: (#15) This method must be called with an app access token.

    any ideia?

    Reply
  10. Julie Carter

    Unknown Object Type: Object at URL ‘http://samples.ogp.me/431410613536548′ is invalid because the configured ‘og:type’ of ’430460170298259:group’ is invalid.

    No Matter what I do, I keep getting this error message when I debug sample data. Any ideas?

    Thanks
    Julie

    Reply
  11. Kpoulianos

    I have the same problem others mentioned. Message (#3502) Object at url…has og:type of ‘website’. The property ‘article’ requires an object of og:type ‘article’.

    Here is the link to the article http://fb.idebate.eu/article2.php.

    Thanks for writing this tutorial, it’s been great help.

    Reply
    1. Hasnain Post author

      You should check your URL in debugger and if it shows no error and og tags are same as of your app then may be its a bug.

      Reply
      1. Kpoulianos

        The debugger indeed showed no errors, but in fact there was an error in the API call wich I fixed, everything is working now. The only problem I still have is that the access token copied and pasted in “step 4″ expires after 2 hours and I need to request a fresh read code, and paste the new token. Any way to do this automatically, or to make tokens not expire?

        Reply
  12. Sana

    Boss I’m getting a Window saying,

    Error occurred

    Type: OAuthException

    Message: An active token must be used to query information about the current user.

    ==>>> Breaking head since noon, Would be helpful if u reply within tomorrow noon, Pleaseee

    Reply
  13. Deb Bhattrai

    hi hassin,
    i’ve read the article carefully. there is no underscore in the code which i got from step 3. so i did not put in the code. i still get error like:

    Error occured

    Type: Exception

    Message: The action you’re trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: cusine.

    Reply
  14. Thy

    There seems to be some error with documentation and the fb.api. After playing around with the code I got it to work. Please view my same code here: nopenonlytext.tnguyen.com/test.html

    note that my fb.api call has been modified to:
    FB.api(
    ‘/me/Publish’,
    ‘post’,
    { article: ‘http://nopenonlytext.tnguyen.com/test.html’ },

    Publish is the Action Type
    article is the object.

    Reply
  15. Pingback: Facebook plug-in for Wordpress « No Pen Only TeXt

Leave a Reply

Your email address will not be published. Required fields are marked *