开发者

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

I have a fan page setup for my company.

I want to automate the posting of regular updates to that page's wall from my C# desktop application.

  • Which Facebook C# library is the simplest?

  • How can I easily acquire the access token for this page?

  • What is the most conci开发者_运维百科se code snippet that will simply allow me to then post to the wall?

I have read through all the docs and millions of stackoverflow and blog posts and it all seems very convoluted. Surely it can't be that hard..

I have setup an "application" within facebook that has its own App ID, API Key and App Secret etc.


@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
        }

    }


}


Check my answer over here: how to post to facebook page wall from .NET

Check full example: http://klaatuveratanecto.com/posting-on-facebook-wall-feed-with-c-and-asp-net-mvc-5/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜