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 concise 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.
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
}
}
}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With