user1018697 user1018697 - 10 days ago 5
ASP.NET (C#) Question

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

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"]))
{
Response.Redirect("https://graph.facebook.com/oauth/authorize?client_id=157873644371673&redirect_uri=http://localhost:2551/Default.aspx&scope=publish_stream,manage_pages,offline_access&display=popup");
}
else
{

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 = "www.mysite.com"
});

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.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "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 = "www.zombo.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};

try
{
var result = fb.Post("/" + "MY_FACEBOOK_FAN_PAGE_ID" + "/feed", parameters);
}
catch (FacebookOAuthException ex)
{
//handle something
Response.Write(ex.Message);
}
}

Answer

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 http://www.mywebsiteurl.com/Default.aspx 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 http://www.facebook.com/pages/toto/446533181408238?ref=ts&fref=ts 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)
            {
                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 = 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('&'))
                    {
                        //meh.aspx?token1=steve&token2=jake&...
                        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.link = "http://www.natiska.com/article.html";
                parameters.picture = "http://www.natiska.com/dav.png";
                parameters.name = "Article Title";
                parameters.caption = "Caption for the link";

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

            }
              }
Comments