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

后端 未结 3 1792
轮回少年
轮回少年 2020-12-30 10:09

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

相关标签:
3条回答
  • 2020-12-30 10:33

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

    0 讨论(0)
  • 2020-12-30 10:36

    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 讨论(0)
  • 2020-12-30 10:54

    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
            }
    
        }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题