Published on: 08 August, 2013

How to post message to facebook in ASP.Net C#

Posted by andy | 13850 views 6 likes 3 favourites 1 comments
Tumblr Digg Google Plus

In this tutorial, we will show how you can post an automatic message to facebook. We will use the SDK C# library released by facebook. If you are using Microsoft Visual Studio, you can easily integrate the facebook reference by installing the sdk via Nugget.

To install it via Nugget, you can go to Tools > Library Package Manager > Package Manager Console

Nugget Package Manager Console

If you do not have Nugget Console Manager, you have to install this plugin for Visual Studio, it is FREE. Once you in the package manager console. You just need to type in Install-Package Facebook

Nugget Install Facebook SDK

Once the installation is completed, the next thing you have to do is to go to facebook developer site https://developers.facebook.com to register an app. You need to register a facebook account, if you do not have one. Once register, you can go to apps menu and there is a button to create new app. Click the button will popup this window.

create facebook app

It will request you to enter the captcha code and you will need to enter your mobile phone number as well in order to approve the new created app. They will send you a verification code in your mobile, so just make sure you enter a correct and valid mobile number.

captcha code

Once the app is created you will see the following image.

facebook apps account page

In order to post a message into user account wall, we need to use Open Graph feature, click the Use Graph API Explorer link as shown above.

You will be redirected to this page. Note: by default the access token is empty, you have to click the button Get Access Token in order to generate this access token.

As we only need to post the message into the user's wall, we only need to tick the user status checkbox as shown below.

facebook access tokens selection

facebook extended permission

This quick code snippet will on how to post the message to the user's wall.

//firstly you will need to import the facebook namespace
//you can double check if facebook.dll file is included in the bin folder. 
//When you install via Nugget, it will automatically place this file in the bin folder.
using Facebook;

private string PostFacebookWall(string accessToken, string message) {
	var responsePost = "";
    try {
		//create the facebook account object
        var objFacebookClient = new FacebookClient(accessToken);
        var parameters = new Dictionary<string, object>();
        parameters["message"] = message;
        responsePost = objFacebookClient.Post("feed", parameters);
    }
    catch (Exception ex) {
        responsePost = "Facebook Posting Error Message: " + ex.Message;
    }
    return responsePost;
}

You may want to check out our other article about posting message to Twitter. Click this article How to post message to Twitter in ASP.Net C#.

Comments
Wayne
Commented on: 02 Jun, 2017 10:26:20 AM
Hello, Andy. Could you please assist me on getting this to work? I would like to post, through a web page, to my companies FB page. However, I was unable to achieve with the code you provided here. Is there more that needs to be done, in order to accomplish the posting to FB? Thank You Wayne
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Share your article with us and add your own google adsense account to earn extra money. Plus, you can promote a link back to your site.
Related Articles
Published on: 13 February, 2017
One thing to remember when publishing your ASP.Net web applications, you may find out, some files could be not completely published due to the settings on the file properties. One example that I had are some font types file. In order to fix this problem you can navigate to the files that are missing and right click the properties.
Published on: 11 May, 2016
The slidingExpiration attribute is used in ASP.Net form authentication. This property is used to reset the expiration time for a valid authentication cookie if a request that has already being made has passed the timeout interval. Which means, if it is already expires, the current user will require to re-authentication again.
Related Tutorials