IMG-LOGO

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

andy - 08 Aug, 2013 30689 Views 3 Comment

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
02 Jun, 2017
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
Ajay kumar
12 Mar, 2018
hi, Wayne! you need to page access token instead of user access token so first you have to get for user access token and then get for page access token like client.Get("me/accounts")
Devindu
21 Feb, 2019
How to import comment from Facebook in ASP.Net C# ?
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.