Apr
22
2011

 

Examples

Here are examples of posting to Facebook well feed using the Javascript Graph API:
http://www.permadi.com/tutorial/facebook-js-graph-api-post-to-wall/index.html

There are two examples there, one using the FB.ui (to show dialog) and the other using FB.api.  The difference is that the FB.api method does not display any user-interface (dialog), so this is best used for canned messages.  

Code

We are going straight to the wall posting code, the initialization of the application can be read here: here and here. Your application needs to have publish_stream permission to post to wall.

Using FB.api

This methods uses the /userId/feed Graph API where userId is the numeric user id of the target (applications can post to the user or the user’s friends wall but not strangers). If you want to post to the wall of the logged in user, you can also use /me/feed. Again, when using this, the posting is done in passive mode (user will not get prompted to enter a message unless you provide your own forms).

[c]
FB.api("/userId/feed", 'post', data, callbackFunction);
[/c]

Here’s a simple working example code:

[c]
	function postToWallUsingFBApi()
	{
		var data=
		{
			caption: 'This is my wall post example',
			message: 'Posted using FB.api',
			link: 'http://wwww.permadi.com/blog/',  
		 }
		FB.api('/me/feed', 'post', data, onPostToWallCompleted);
	}

	// The id of the post will be in response.id
	function onPostToWallCompleted(response)
	{
		// Just show error message if there's an error
		if (response)
		{
			if (response.error)
			{
				alert(response.error.message);
			}
			else
			{
				alert("Posted as "+response.id);		
			}
		}
		// user cancelled
	}
[/c]

The result looks like this.

Notice the blank vertical gray bar on the left side which is the default image because we did not put any picture in the post. To post with picture, continue to the second part of this guide, which is linked at the bottom of this article.

Using FB.ui

This method uses the so-called publish.stream dialog, which has been present since the Old Legacy API. To call this in the Graph API, use FB.ui like below:

[c]
	function postToWallUsingFBUi()
	{	
		var data=
		{
			method: 'stream.publish',
			message: "Posted using FB.ui.",
			display: 'iframe'	
		}
		FB.ui(data, onPostToWallCompleted);
	}

	// The id of the post will be in response.post_id
	function onPostToWallCompleted(response)
	{
		// Just show error message if there's an error
		if (response)
		{
			if (response.id)
			{
				alert("Posted as "+response.post_id);		
			}
			else
			{
				alert("Error");		
			}
		}
		// user cancelled
	}
[/c]

If you’re posting to friend’s wall, use to parameter with that friend’s user id, as described here: http://developers.facebook.com/docs/reference/dialogs/feed/. If you are posting to the current user’s wall, then the userId can be set as me, as in /me/feed. There are many more parameters here, such as to attach picture and links, which you can read at: http://developers.facebook.com/docs/reference/rest/stream.publish/. There are some display_options for the display parameter, such as ‘iframe’, or ‘popup’. The default is ‘iframe’.

In user clicks Publish, then the result on the user’s wall will look like this.

Notes:

Here’s the link to the example again:http://www.permadi.com/tutorial/facebook-js-graph-api-post-to-wall/index.html (You need to replace the APP_ID with yours if you’re using this example on you app.

Next: Publishing Feed With Picture.

Leave a Reply

*