Post Status And Image On Facebook Wall Using ASP.NET

To post status on Facebook using ASP.NET, we must have a valid Facebook account. We need to create a Facebook App first to get App Id and Secret Key.

Steps to create Facebook App to receive App Id and Secret Key

In order to get an App Id and Secret key we need to register a new application

Visit Facebook Developer Page as in the following screenshot:

 

Register yourself as Facebook Developer.

 

After registration as Facebook Developer,

 

Click Register:

 

Create New App,

We will choose Website as a platform to get started and thereby App Id and Secret Key will be generated.

 

After selecting Website as a platform, the following screen appears. In the top right, look for the button “Skip and Create App ID” and click it.

 

Clicking the “Skip and Create App ID” button will bring up the following screen.

 

You need to fill out Display Name and select a Category. Here we gave “Traffic Cop” display name.

You can ignore the Namespace field as this is not necessary for our purpose. Select the category of the app. Here we used News as the category of the app.

 

After following above steps, we will be redirected to the following screen. On this screen we can see App Id and App Secret in two fields at the top. App Secret is in password format. Click the “Show” button, this will display App Secret Key.

 

After clicking Show button, we can see the App Secret Key thereby can use in in our application.

 

These Facebook App Id and App Secret Key is used in the application from where we wish to make Facebook post.

Now set the Site Url, for this go to Settings add the Site URL.

After applying App Id and App Secret, we can now post on Facebook wall.

Let's say, we have the following front end screen.

Front-End 
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title></title>  
  4.     <link href="bootstrap.min.css" rel="stylesheet" />  
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server" style="margin-top:40px;">  
  8.         <div>  
  9.               
  10.     <asp:FileUpload ID="FileUpload1" runat="server" style="margin-left: 538px;"  />  
  11.     <br />  
  12.     <br />  
  13.               
  14.     <asp:TextBox ID="txtMessage" runat="server" TextMode = "MultiLine" CssClass="col-xs-4" style="margin-left: 538px;" ></asp:TextBox>  
  15.     <br />  
  16.     <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick = "UploadPhoto" style="margin-left: 538px;" CssClass="btn" />  
  17.             </div>  
  18.     </form>  
  19. </body>  
  20. </html>  
 

User can choose image file and add caption to the same and upload it to the Facebook wall using Facebook App.

 

Backend Snippet

  1. public partial class CS : System.Web.UI.Page  
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.         FaceBookConnect.API_Key = "enter App Id here";  
  6.         FaceBookConnect.API_Secret = "enter App Secret here";  
  7.         if (!IsPostBack)  
  8.         {  
  9.               
  10.             string code = Request.QueryString["code"];  
  11.             if (!string.IsNullOrEmpty(code))  
  12.             {  
  13.   
  14.                 if (Session["File"] != null)  
  15.                 {  
  16.                     FaceBookConnect.PostFile(code, "me/photos", (HttpPostedFile)Session["File"], Session["Message"].ToString());  
  17.                 }  
  18.                 else  
  19.                 {  
  20.                     Dictionary<stringstring> data = new Dictionary<stringstring>();  
  21.                     data.Add("message", Session["Message"].ToString());  
  22.                     FaceBookConnect.Post(code, "me/feed", data);  
  23.                 }  
  24.                  
  25.                  
  26.                 Session["File"] = null;  
  27.                 Session["Message"] = null;  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32.     protected void UploadPhoto(object sender, EventArgs e)  
  33.     {  
  34.         if (FileUpload1.HasFile)  
  35.         Session["File"] = FileUpload1.PostedFile;  
  36.         Session["Message"] = txtMessage.Text;  
  37.         FaceBookConnect.Authorize("user_photos,publish_actions", Request.Url.AbsoluteUri.Split('?')[0]);  
  38.     }  
  39. }   
We can see that photo is uploaded on the Facebook profile with the caption.

 
Closure

In this way, we implemented a demo how we can post on Facebook using App Id and App Secret account of Facebook App. Hope you like it.
 
Read more articles on ASP.NET:


Similar Articles