使用C#发布到Facebook粉丝页面的最简单方法!

我为我的公司设置了粉丝页面。

我想从C#桌面应用程序自动发布对该页面墙的定期更新。

  • 哪个Facebook C#库最简单?

  • 如何轻松获取此页面的访问令牌?

  • 什么是最简洁的代码片段,只允许我发布到墙上?

我已经阅读了所有文档和数百万的stackoverflow和博客文章,这一切看起来都很复杂。 当然不会那么难……

我在facebook中设置了一个“应用程序”,它有自己的App ID,API Key和App Secret等。

@Aaron – 最好的图书馆是facebook c#sdk。 我每天都在使用它……因为我的公司写了它,所以我很有偏见 – 但它是一个动态库,并且从Facebook(每周二)的更新速度来看,它非常适合可扩展的开发。

http://facebooksdk.codeplex.com/

我不会用它进行身份validation – 就像在codeplex上有很多例子: http ://facebooksdk.codeplex.com/wikipage?title = Code%20Examples&referringTitle=Documentation但是在你经过身份validation后对页面进行发布并有一个访问令牌,代码将是这样的:

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 (ie: 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 } 

希望这可以帮助。

我发布这个是因为互联网上缺乏良好的信息导致我花费的时间超过了我的需要。 我希望这会使其他人受益。 关键是将&scope = manage_pages,offline_access,publish_stream添加到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 } } } 

在这里查看我的答案: 如何从.NET发布到Facebook页面墙

查看完整示例: http : //klaatuveratanecto.com/posting-on-facebook-wall-feed-with-c-and-asp-net-mvc-5/