Simplest way to post to a facebook fan page's wall with C#!

@Aaron - the best library is the facebook c# sdk. I use it every day... granted I am biased as my company writes it - but it's a dynamic library and with the rate of updates from Facebook (every Tuesday) it is well suited for scalable development.

http://facebooksdk.codeplex.com/

I won't get into authentication with it - as on codeplex there are many examples: http://facebooksdk.codeplex.com/wikipage?title=Code%20Examples&referringTitle=Documentation But to do a post to a page, after you have authenticated and have an access token, the code would be something like this:

dynamic messagePost = new ExpandoObject();
messagePost.access_token = "[YOUR_ACCESS_TOKEN]";
messagePost.picture = "[A_PICTURE]";
messagePost.link = "[SOME_LINK]";
messagePost.name = "[SOME_NAME]";
messagePost.caption = "{*actor*} " + "[YOUR_MESSAGE]"; //<---{*actor*} is the user (i.e.: Aaron)
messagePost.description = "[SOME_DESCRIPTION]";

FacebookClient app = new FacebookClient("[YOUR_ACCESS_TOKEN]");

try
{
    var result = app.Post("/" + [PAGE_ID] + "/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
     //handle something
}
catch (FacebookApiException ex)
{
     //handle something else
}

Hope this helps.


I am posting this because of lack of good information on the internet that led to me spend more time than I needed. I hope this will benefit others. The key is adding &scope=manage_pages,offline_access,publish_stream to the url.

class Program
{
    private const string FacebookApiId = "apiId";
    private const string FacebookApiSecret = "secret";

    private const string AuthenticationUrlFormat =
        "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=manage_pages,offline_access,publish_stream";

    static void Main(string[] args)
    {
        string accessToken = GetAccessToken(FacebookApiId, FacebookApiSecret);

        PostMessage(accessToken, "My message");
    }

    static string GetAccessToken(string apiId, string apiSecret)
    {
        string accessToken = string.Empty;
        string url = string.Format(AuthenticationUrlFormat, apiId, apiSecret);

        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();

        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            String responseString = reader.ReadToEnd();

            NameValueCollection query = HttpUtility.ParseQueryString(responseString);

            accessToken = query["access_token"];
        }

        if (accessToken.Trim().Length == 0)
            throw new Exception("There is no Access Token");

        return accessToken;
    }

    static void PostMessage(string accessToken, string message)
    {
        try
        {
            FacebookClient facebookClient = new FacebookClient(accessToken);

            dynamic messagePost = new ExpandoObject();
            messagePost.access_token = accessToken;
            //messagePost.picture = "[A_PICTURE]";
            //messagePost.link = "[SOME_LINK]";
            //messagePost.name = "[SOME_NAME]";
            //messagePost.caption = "my caption"; 
            messagePost.message = message;,
            //messagePost.description = "my description";

            var result = facebookClient.Post("/[user id]/feed", messagePost);
        }
        catch (FacebookOAuthException ex)
        {
             //handle something
        }
        catch (Exception ex)
        {
             //handle something else
        }

    }


}

Tags:

C#

Facebook

Api

Sdk