Category : Javascript | Author : Chtiwi Malek | First posted : 5/8/2012 | Updated : 5/8/2012
Tags : facebook, api, graph, post, wall, page, public, javascript, code, feed, link, share
Post on a user wall or a facebook page using FB.api

Post on a user wall or a facebook page using FB.api

In this post we will see how to make a full wall post to a User's Profile or to a public facebook page using the Facbook javascript Graph API.
The wall post will contain this fields : a Message, Name, Description, Link, Picture and Caption.

After authenticating the User (you need the publish_stream permission), here's what to do:
	var params = {};
	params['message'] = 'The message';
	params['name'] = 'Name';
	params['description'] = 'this is a description';
	params['link'] = 'http://www.somelink.com/page.htm';
	params['picture'] = 'http://www.somelink.com/img/pic.jpg';
	params['caption'] = 'Caption of the Post';
	 
	FB.api('/me/feed', 'post', params, function(response) {
	  if (!response || response.error) {
	    // an error occured
	    alert(JSON.stringify(response.error));
	  } else {
	    // Done
	    alert('Published to stream');
	  }
	});
If you want to post on a facebook page wall instead of users wall (you need to get manage_pages permission from the user), just replace the FB.api line with:

... FB.api('/page-id/feed', 'post', params, function(response) { ...

The example above will publish a post not a Link, if you want to post a Link/Url use this code:
	var params = {};
	params['message'] = 'The message';
	params['link'] = 'http://www.somelink.com/page.htm';
	 
	FB.api('/me/links', 'post', params, function(response) {
	  if (!response || response.error) {
	    // an error occured
	    alert(JSON.stringify(response.error));
	  } else {
	    // Done
	    alert('Link Published to stream');
	  }
	});
Comments & Opinions :
Yudi
works great, thank you so much. Thanks!
- by Yudi on 10/16/2012
and for groups??
Hi, I was wondering how clould you do it for a group instead a page, i tried replacing page-id with group-id but it doesn't work,
i have "an unespected error" and then i add the user's token but still get the same error,
do you know how to do it ?
- by Paco on 12/18/2012
Great Post!
Extremely useful post!
- by Priyantha on 4/16/2016
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :


» Member access