Calling Facebook RESTful API using C# (Without Microsoft Facebook SDK)
To continue with our previous posts about Facebook I’m going explain how to use the RESTful API from C# code. This time I’m writing a console application to read user Facebook’s statuses. This illustrates that you can really use the RESTful API from any application including desktop.
A very important thing you need to do before this code works is to ask your user for authorization to read his stream with offline access as shown in this post. After your user grants your application to have offline access you can read his session_key and use it everywhere because it will never expire.
First we need to write a Class that mimics Status FQL table.
public class Status
{
public String status_id { get; set; }
public String time { get; set; }
public String source { get; set; }
public String message { get; set; }
}
Then we create a method to generate our data signature.
public static String GetSignature(Dictionary parameters)
{
MD5 md5 = MD5.Create();
String data = "";
String[] keys = parameters.Keys.OrderBy(k => k.ToString()).ToArray();
for (int i = 0; i < keys.Length; i++)
{
String key = keys[i];
String value = parameters[key];
data += key + "=" + value;
}
data += "APPLICATION_SECRET";
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(data));
String signature = "";
foreach (byte b in bytes) signature += b.ToString("x2");
return signature;
}
After that we just build our post data, post our request, read our response and deserialize the status objects.
static void Main(string[] args)
{
Dictionary parameters = new Dictionary();
parameters.Add("format", "json");
parameters.Add("method", "Fql.query");
parameters.Add("query", "select status_id, time, source, message from status where uid = USER_ID");
parameters.Add("session_key", "SESSION_KEY");
parameters.Add("api_key", "API_KEY");
parameters.Add("v", "1.0");
parameters.Add("call_id", DateTime.Now.Ticks.ToString());
parameters.Add("sig", GetSignature(parameters));
String postData = "";
for (int i = 0; i < parameters.Keys.Count; i++)
{
String key = parameters.Keys.ElementAt(i);
String value = parameters[key];
String param = key + "=" + HttpUtility.UrlEncode(value);
postData += param;
if (i < parameters.Keys.Count - 1) postData += "&";
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.facebook.com/restserver.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData);
writer.Close();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Status[]));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Status[] statuses = (Status[])serializer.ReadObject(response.GetResponseStream());
foreach (Status s in statuses)
{
DateTime time = new DateTime(long.Parse(s.time));
Console.WriteLine(time.ToString() + ", " + s.message);
}
Console.ReadLine();
}
Note that you can use this approach to call any method in the RESTful API, not to just make FQL queries.
No comments:
Post a Comment