How to get an access token using C# SDK

avnic picture avnic · Mar 28, 2012 · Viewed 51.3k times · Source

I updated the Facebook SDK to 6.0.10 and some code that used to work is not working anymore. I used to post on users' wall use the method below.

The class FacebookClient used to take the AppId, and AppSecret and I didn't gave it any access token for my application.

string uId = "SomeUid";
FacebookClient fb = new FacebookClient(AppId,AppSecret );

string userFeedPath = String.Format("/{0}/feed", uId);

dynamic parameters = new ExpandoObject();
parameters.link = "Test@test";
parameters.message = "test";

try
{
    dynamic result = fb.Post(userFeedPath, parameters);
}
catch(Exception ex)
{

}

Now even if I try this,

FacebookClient fb = new FacebookClient();

fb.AppId = "1234...";
fb.AppSecret = "76e69e0c334995cecc8c....";

I'm getting this error:

(OAuthException - #200) (#200) This API call requires a valid app_id.

How do I fix this problem?

Answer

prabir picture prabir · Mar 28, 2012

You will need to get the app access token by making the request.

var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new { 
    client_id     = "app_id", 
    client_secret = "app_secret", 
    grant_type    = "client_credentials" 
});
fb.AccessToken = result.access_token;