Posting to Facebook with C#

In this post I’ll walk through the steps required to register a Facebook app, and post to your own Facebook wall using C# and Visual Studio 2010.

Step 1:
Before starting, you’ll need to log on to the Facebook developer site and confirm your Facebook profile if you haven’t already done so:
www.facebook.com/help/verify

Step 2:
Next, select Apps from the toolbar, then select +Create New App:

Step 3:
Enter the display name and namespace for the application:

Step 4:
Add the contact email address and category, or add any additional administrator accounts:

Step 5:
Open Visual Studio 2010 and, using NuGet, download and install the Facebook package:

Step 6:
Since the application is only posting to our own account, the access keys can be generated up-front. This can be done using the Graph API Explorer. Open the Graph API Explorer and change the application dropdown from “Graph API Explorer” to the name of your app.

Select “Get Access Token”. For this to work, three additional permissions are required:

  • publish_stream – Enables your app to post content, comments, and likes to a user’s stream and to the streams of the user’s friends
  • manage_pages – Enables your application to retrieve access_tokens for pages the user administrate
  • offline_access – Enables your app to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.

Copy the access token for later. For more information on Facebook permissions check out their permissions page.

Step 7 – Posting to Your Wall:
Using the Facebook package, and the access token from Step 6, we can now post a comment to your Facebook wall:

private static void PostToWall(string message, long userId, string wallAccessToken)
{
    var fb = new FacebookClient(wallAccessToken);
    string url = string.Format("{0}/{1}", userId, "feed");
    var argList = new Dictionary<string, object>();
    argList["message"] = message;
    fb.Post(url, argList);
}

Under the hood the Facebook Client is sending a HTTP POST request to Facebook’s graph API. The actual URL is:
https://graph.facebook.com/USER_ID/feed?message=MESSAGE&access_token=TOKEN

Step 8 – Posting to a Page:
Posting to a Facebook page that you own is just as simple, but you’ll need to create an access token using the Graph API Explorer. Each page has it’s own access token, and Facebook uses this to determine which page you are posting to.

private static void PostToPage(string message, string pageAccessToken)
{
    var fb = new FacebookClient(pageAccessToken);
    var argList = new Dictionary<string, object>();
    argList["message"] = message;
    fb.Post("feed", argList);
}

This post doesn’t go into much detail, that’s intentional as this is as far as my knowledge goes on the subject. For more information I’d recommend you visit the Facebook Docs or the brilliant StackOverflow.

Resources:
NuGet
Facebook Developer Site
Facebook Permissions
Facebook Graph API Explorer
StackOverlow – Post on a Facebook business page from another user app

Advertisements
This entry was posted in Reference and tagged , , . Bookmark the permalink.

6 Responses to Posting to Facebook with C#

  1. Dmitriy says:

    ok and how to post image?

  2. Nags says:

    Hi i am using the above code to post on facebook wall but my newline characters are displayed on wall(for eg: line 1 \nline 2) instead of
    line 1
    line 2
    how to handle it please help

  3. hnieef says:

    hi how to send private message .

  4. Pingback: [RESOLVED]how to use f-commenrce? | ASP Questions & Answers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s