Post on Facebook Users Wall Using ASP.Net C#

This article demonstrates how to post content (status, image, video, URL) to a user's Facebook wall using ASP.Net. Since I was thinking of implementing this feature in my current project, I found something cool and decided to share it with C# Corner readers.

Getting Started

First of all create a new website in ASP.Net using C#. And now login into Facebook and click on the developer link and create a new app and supply an app name and click "Continue".

img1.jpg

Image1.

Now supply the captcha code in the TextBox and click "Continue".

img2.jpg

Image 2.

As you can see, there is an AppID and App Secret. You must use these so copy them. And supply the site URL and click "Save changes".

img3.jpg

Image 3.

Now let's start working on the UI. In this sample I will explain two ways to post a feed.

First scenario

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

    <div id="fb-root">

    </div>

    <script>

        window.fbAsyncInit = function () {

            FB.init({ appId: '491677990891136', status: true, cookie: true,

                xfbml: true

            });

        };

        (function () {

            var e = document.createElement('script'); e.async = true;

            e.src = document.location.protocol +

'//connect.facebook.net/en_US/all.js';

            document.getElementById('fb-root').appendChild(e);

        } ());

    </script>

    <script type="text/javascript">

        $(document).ready(function () {

            $('#share_button').click(function (e) {

                e.preventDefault();

                FB.ui(

{

    method: 'feed',

    name: 'jQuery Dialog',

    link: 'http://www.c-sharpcorner.com/authors/raj1979/raj-kumar.aspx',

    picture: 'http://www.c-sharpcorner.com/UploadFile/AuthorImage/raj1979.gif',

    caption: 'This article demonstrates how to use the jQuery dialog feature in ASP.Net.',

    description: 'First of all make a new website in ASP.Net and add a new stylesheet and add .js files and put images in the images folder and make a reference to the page.',

    message: 'hello raj message'

});

            });

        });

    </script>

 

 

<div>

        <input type="button" id="share_button" value="Share" />

    </div>

Run application

img4.jpg

Image 4.

Type a message and click "Share".

img5.jpg

Image 5.

Login to Facebook and see wall posts.

Second scenario

Add the Facebook client assembly in the bin folder and work on the code behind.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;

using System.IO;

using Facebook;

 

public partial class Default2 : System.Web.UI.Page

{

    string bodymessage = "This is test message description new";

    protected void Page_Load(object sender, EventArgs e)

    {

        CheckAuthorization();

    }

 

    private void CheckAuthorization()

        {

            string app_id = "APP_ID";

            string app_secret = "APP_SECRET";

            string scope = "publish_stream,manage_pages";

 

            if (Request["code"] == null)

            {

                Response.Redirect(string.Format(

                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",

                    app_id, Request.Url.AbsoluteUri, scope));

            }

            else

            {

                Dictionary<string, string> tokens = new Dictionary<string, string>();

 

                string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",

                    app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

 

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

 

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

                {

                    StreamReader reader = new StreamReader(response.GetResponseStream());

 

                    string vals = reader.ReadToEnd();

 

                    foreach (string token in vals.Split('&'))

                    {                       

                        tokens.Add(token.Substring(0, token.IndexOf("=")),

                            token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));

                    }

                }

 

                string access_token = tokens["access_token"];

 

                var client = new FacebookClient(access_token);

 

                client.Post("/me/feed", new { message = bodymessage });

            }

        }   

}

Run the application to see the result.

img6.jpg

Image 6.

 

Up Next
    Ebook Download
    View all
    Learn
    View all