Facebook C# SDK - Making Requests
This blog post is gonna be all about codes and how you can use FacebookClient class to make synchronous requests to the Facebook server. But first …
The basics
To access most of the protected resource in Facebook, you require access token. You can read more about it on how to get the access token in this post.
Install-Package Facebook
Graph Api (GET)
var fb = new FacebookClient();
var result = (IDictionary<string,object>)fb.Get("4");
var id = (string)result["id"];
var name = (string)result["name"];
var firstName = (string)result["first_name"];
var lastName = (string)result["last_name"];
var link = (string)result["link"];
var username = (string)result["username"];
var gender = (string)result["gender"];
var male = (string)result["locale"];
Passing Parameters
var fb = new FacebookClient();
var parameters = new Dictionary<string, object>();
parameters["fields"] = "id,name";
var result = (IDictionary<string, object>)fb.Get("4", parameters);
var id = (string)result["id"];
var name = (string)result["name"];
Note:
- Cast to
IDictionary<string, object>
if it is a json object, - Cast to
IList<object>
if it is is a json array, - Cast to
string
,long
,double
orboolean
accordingly depending on the json primitive types, or - use dynamic without explicitly casting. (supported only in frameworks where dynamic keyword is supported.)
Using dynamic
var fb = new FacebookClient();
dynamic result = fb.Get("4");
var id = result.id;
var name = result.name;
var firstName = result.first_name;
var lastName = result.last_name;
var link = result.link;
var username = result.username;
var gender = result.gender;
var male = result.locale;
Passing parameters the dynamic way.
(Use ExpandoObject
instead of IDictionary<string, object>
)
var fb = new FacebookClient();
dynamic parameters = new ExpandoObject();
parameters.fields = "id,name";
dynamic result = fb.Get("4", parameters);
var id = result.id;
var name = result.name;
Graph Api (POST)
var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.message = "Hello World!"
dynamic result = fb.Post("me/feed", parameters);
var id = result.id;
Graph Api (DELETE)
var fb = new FacebookClient("access_token");
dynamic result = fb.Delete(id);
Legacy REST API
Unlike graph api, you need to only pass parameters and make sure you set the “method” in the parameter.
var fb = new FacebookClient();
dynamic parameters = new ExpandoObject();
parameters.method = "users.getInfo";
parameters.uids = "4";
parameters.fields = new[] { "name", "first_name", "last_name" };
dynamic result = fb.Get(parameters);
FQL
var fb = new FacebookClient();
string query = string.Format("SELECT name FROM user WHERE uid='{0}'", 4);
dynamic result = fb.Query(query);
FQL-MultiQuery
var fb = new FacebookClient();
string query0 = string.Format("SELECT first_name FROM user WHERE uid='{0}'", "4");
string query1 = string.Format("SELECT last_name FROM user WHERE uid='{0}'", "4"); ;
dynamic result = fb.Query(query0, query1);
var result0 = result[0].fql_result_set;
var result1 = result[0].fql_result_set;
Batch Requests
Well … this one requires a whole new blog post. So here it goes
Getting user profile picture
Lot of devs have been asking how to retrieve the user profile picture. You don’t need to use FacebookClient to retrieve the picture. Just get the user id and generate the url for the picture.
string profilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture", user_id);
Using Access Token
Most of the above samples were shown without using the access token, but most of the api requests requires using the access token. You can set the access token in the following ways.
var fb = new FacebookClient("access_token");
or you could also use the AccessToken property.
var fb = new FacebookClient();
fb.AccessToken = "access_token";
Using Application Access Token
There are times when you need to use the application access token instead of the user access token.
var oauthClient = new FacebookOAuthClient
{
AppId = "app_id",
AppSecret = "app_secret"
};
dynamic result = oauthClient.GetApplicationAccessToken();
string appAccessToken = result.access_token;
or you could also use the application access token as
string appAccessToken = string.Concat(appId, "|", appSecret);
or use the overload constructor of FacebookClient. The below code also sets the app access token automatically for you using the above method.
var fb = new FacebookClient("app_id", "app_secret");
OAuth 2.0 - exchange code for access token
FacebookClient supports parsing only json responses. Due to this reason oauth/access_token
token will not work when using FacebookClient.Get("oauth/access_token")
. Instead you will need to use a method in FacebookOAuthClient
.
var oauthClient = new FacebookOAuthClient
{
AppId = "app_id",
AppSecret = "app_secret",
RedirectUri = new Uri("http://redirecturi.com")
};
dynamic result = oauthClient.ExchangeCodeForAccessToken("code");
var accessToken = result.access_token;
Handling Exceptions
If you want to create apps that are error free, make sure to catch exceptions that are thrown when you make requests to the facebook server.
try
{
var fb = new FacebookClient();
dynamic result = fb.Get("4");
var name = result.name;
}
catch (FacebookApiException ex)
{
}
catch (Exception ex)
{
}
You could also catch more generic FacebookApiException
and take actions accordingly.
try
{
var fb = new FacebookClient();
dynamic result = fb.Get("4");
var name = result.name;
}
catch (FacebookApiLimitException ex)
{
}
catch (FacebookOAuthException ex)
{
}
catch (FacebookApiException ex)
{
}
catch (Exception ex)
{
}