我一直在使用Facebook C#SDK,但有一个非常旧的版本,我仍在使用REST API(我认为)。我只关心使用API发布到我自己的Facebook页面(我有一个WCMS插件,可以将内容发布到我们机构的Facebook页面)。我想知道是否有人知道如何使用最新版本的SDK进行此设置的好教程?我也关注当offline_access消失时这是如何工作的,所以对此的任何想法都会受到赞赏。我宁愿不必每60天手动获取一个新的访问令牌。这似乎有点不必要,因为我用来发布的应用程序与我试图发布的页面位于同一个FB帐户中。
答案 0 :(得分:12)
这是一种方法:
public static string RefreshTokenAndPostToFacebook(string currentAccessToken)
{
string newAccessToken = RefreshAccessToken(currentAccessToken);
string pageAccessToken = GetPageAccessToken(newAccessToken);
PostToFacebook(pageAccessToken);
return newAccessToken; // replace current access token with this
}
public static string GetPageAccessToken(string userAccessToken)
{
FacebookClient fbClient = new FacebookClient();
fbClient.AppId = "app id";
fbClient.AppSecret = "app secret";
fbClient.AccessToken = userAccessToken;
Dictionary<string, object> fbParams = new Dictionary<string, object>();
JsonObject publishedResponse = fbClient.Get("/me/accounts", fbParams) as JsonObject;
JArray data = JArray.Parse(publishedResponse["data"].ToString());
foreach (var account in data)
{
if (account["name"].ToString().ToLower().Equals("your page name"))
{
return account["access_token"].ToString();
}
}
return String.Empty;
}
public static string RefreshAccessToken(string currentAccessToken)
{
FacebookClient fbClient = new FacebookClient();
Dictionary<string, object> fbParams = new Dictionary<string, object>();
fbParams["client_id"] = "app id";
fbParams["grant_type"] = "fb_exchange_token";
fbParams["client_secret"] = "app secret";
fbParams["fb_exchange_token"] = currentAccessToken;
JsonObject publishedResponse = fbClient.Get("/oauth/access_token", fbParams) as JsonObject;
return publishedResponse["access_token"].ToString();
}
public static void PostToFacebook(string pageAccessToken)
{
FacebookClient fbClient = new FacebookClient(pageAccessToken);
fbClient.AppId = "app id";
fbClient.AppSecret = "app secret";
Dictionary<string,object> fbParams = new Dictionary<string,object>();
fbParams["message"] = "Test message";
var publishedResponse = fbClient.Post("/your_page_name/feed", fbParams);
}
答案 1 :(得分:2)
我建议你先阅读这篇博文。 http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-First-Facebook-Application-v6.aspx
答案 2 :(得分:1)
http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx
找到图表api帖子
我希望有一天prabir和nathan能够完成网站文档,我们将会清楚地了解它。现在就开始浏览网页。