1

I want to update the facebookpage using c# sdk. I have partially successful with this, the problem is whenever I post messages to the page, post is visible only for admin(i am the admin of the page)is logged In. I want the post or feed to be visible to every one who visit the page. (even admin is logged out post's are not visible to admin also)

The following code i am trying to achieve

public ActionResult FacebookPagePost()
{
            string app_id = "xxxx";
            string app_secret = "xxx";
            string scope = "publish_stream,manage_pages";
            string page_Id = "xxX";
            if (Request["code"] == null)
            {
                return Redirect(string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                try
                {

                    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);
                    dynamic fbAccounts = client.Get("/me/accounts");


                    dynamic messagePost = new ExpandoObject();
                    messagePost.picture = "http://pic.com/pic.png";
                    messagePost.link = "http://www.examplearticle.com";
                    messagePost.name = "name goes here";
                    messagePost.description = "description goes here";

                    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
                    foreach (dynamic account in fbAccounts.data) {
                        if (account.id == page_Id)
                        {
                            //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out.
                            messagePost.access_token = account.access_token;
                            break;
                        }
                    }

                    client.Post("/" + page_Id + "/feed", messagePost);
                }
                catch (FacebookOAuthException ex)
                {

                }
                catch (Exception e)
                {

                }
            }
}
Deepak
  • 13
  • 3

1 Answers1

0

1) Create a Facebook App at: developers.facebook.com and get yourself an APPID and APPSECRET. (there are a lot of tutorials online for doing this so I will skip repeating it)

2) Go to: http://developers.facebook.com/tools/explorer and choose your app from the dropdown and click "generate access token".

3) After that do the following steps here: https://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token to get yourself a permanent page token. (I can not stress this enough, follow the steps carefully and thoroughly)*

*I have tool I built that does this for me, all I enter is the APPID, APPSECRET and ACCESSTOKEN which the tool then generates a permanent page token for me. Anyone is welcomed to use it and help make it better,

https://github.com/devfunkd/facebookpagetokengenerator

=========================================================================

Ok at this point you should have your APPID, APPSECRET and a PERMANENT PAGE TOKEN.

=========================================================================

In your Visual Studio solution:

4) Using Nuget:Install-Package Facebook

5) Implement the Facebook client:

public void PostMessage(string message)
        {
            try
            {
                var fb = new FacebookClient
                {
                    AppId = ConfigurationManager.AppSettings.Get("FacebookAppID"),
                    AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
                    AccessToken = ConfigurationManager.AppSettings.Get("FacebookAccessToken")
                };

                dynamic result = fb.Post("me/feed", new
                {
                    message = message
                });
            }
            catch (Exception exception)
            {
                // Handle your exception
            }
        }  

I hope this helps anyone who is struggling to figure this out.

Community
  • 1
  • 1
devfunkd
  • 2,929
  • 10
  • 42
  • 67