user1018697 user1018697 - 8 months ago 65
ASP.NET (C#) Question

How to do a post in facebook on my page fan wall with c# and

I tryed since 3 days how i create a post my fan page wall with c#, and i note 2 things :
- Facebook provides not updated documentation with no complete and poor example (api changes often)
- Facebook often changes his api and lots of post are obselete.
Is somebody can correct my code or provide me the complete good code ?

This is my code :

if (String.IsNullOrEmpty(Request.QueryString["code"]))

FacebookClient fb = new FacebookClient();
dynamic result1 = fb.Get("oauth/access_token", new
client_id = "MY_APP_ID",
client_secret = "MY_SECRET_ID",
grant_type = "client_credentials",
redirect_uri = ""

fb.AppId = "MY_APP_ID";
fb.AppSecret = "MY_SECRET_ID";
fb.AccessToken = result1.access_token;

dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article"; = "";
parameters.picture = ""; = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.req_perms = "manages_pages";
parameters.scope = "manages_pages";

parameters.actions = new
name = "View on Zombo",
link = "",
parameters.privacy = new
value = "ALL_FRIENDS",

var result = fb.Post("/" + "MY_FACEBOOK_FAN_PAGE_ID" + "/feed", parameters);
catch (FacebookOAuthException ex)
//handle something

Answer Source

I hope this post will be helpfull for lots of people, i try to be simple and clear :

1-Create your facebook developper account, and to test your code in your computern (localhost), set your localhost adress in the field "website authentication with facebook authentication". For me it will be http://localhost:2551/Default.aspx for example because i test in the Defaut.aspx of my wweb application. You 'll change this adress when you'll deploy on your website (for me i ll change with just before deploy the code on my website).

2- With your facebook user account, create your fan page.

3-When you created your fan page , go to your fan page see the URL to obtain your PAGE_ID For example mine is so my PAGE_ID is 446533181408238

3- It's almost finished, just a little explanation : because i created the fan page , i m administrator of the fan page and i must ask the authoraization to facebook to post since my developper account so i must get the autorisation for 2 actions : publish_stream and manage_pages.

Let's go for coding :

  private void Do()
            string app_id = "157873644371675";
            string app_secret = "c27a10c347af4280720fa3d76c9ae08c";
            string scope = "publish_stream,manage_pages";

            if (Request["code"] == null)
                    app_id, Request.Url.AbsoluteUri, scope));
                Dictionary<string, string> tokens = new Dictionary<string, string>();

                string url = string.Format("{0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                    app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

                HttpWebRequest request = System.Net.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);

                dynamic parameters = new ExpandoObject();
                parameters.message = "Check out this funny article";
       = "";
                parameters.picture = "";
       = "Article Title";
                parameters.caption = "Caption for the link";

                //446533181408238 is my fan page
                client.Post("/446533181408238/feed", parameters);