我为我的公司设置了粉丝页面。
我想自动从我的C#桌面应用程序发布对该页面墙的定期更新。
哪个Facebook C#库最简单?
如何轻松获取此页面的访问令牌?
什么是最简洁的代码片段,只允许我发布到墙上?
我已经阅读了所有文档和数百万个stackoverflow和博客帖子,这一切看起来都很复杂。当然不会那么难......
我在facebook中设置了一个“应用程序”,它有自己的App ID,API Key和App Secret等。
答案 0 :(得分:10)
http://facebooksdk.codeplex.com/
我不会用它进行身份验证 - 就像在codeplex上有很多例子: http://facebooksdk.codeplex.com/wikipage?title=Code%20Examples&referringTitle=Documentation 但是要对页面进行发布,在进行身份验证并拥有访问令牌后,代码将是这样的:
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
}
希望这有帮助。
答案 1 :(得分:9)
由于互联网上缺乏良好的信息导致我花费的时间超过了我的需要,因此我发布此信息。我希望这会使其他人受益。关键是将&amp; scope = manage_pages,offline_access,publish_stream添加到网址。
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
}
}
}
答案 2 :(得分:0)