Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an access token using C# SDK

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?

like image 695
avnic Avatar asked Mar 28 '12 11:03

avnic


People also ask

How do I get my access token?

After you have added an OAuth1 profile to the request, you need to configure it. This topic describes the settings and menus you use to configure OAuth 1.0 authorization. To use OAuth1 authorization in requests, you need to specify the Access Token and Token Secret (access token secret) values.

What is access token in C#?

OAuth is a token based authorization mechanism for REST Web API. You develop the authorization with the API only once up until the expiration time of the token. The generated token is then used each time the REST Web API is called, saving an authorization step every time the REST Web API is called.

How can I get access token authorization code?

The authorization code grant is used when an application exchanges an authorization code for an access token. After the user returns to the application via the redirect URL, the application will get the authorization code from the URL and use it to request an access token.

How do I get an OAuth 2.0 authentication token in C#?

I have these settings: Auth URL (which happens to be a "https://login.microsoftonline.com/...") if that helps. Access Token URL "https://service.endpoint.com/api/oauth2/token"


2 Answers

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;
like image 198
prabir Avatar answered Sep 19 '22 01:09

prabir


For code -> user access token exchange in server-side flow - instead (ver. 5):

    FacebookOAuthClient oAuthClient = new FacebookOAuthClient();
    oAuthClient.AppId = "...";
    oAuthClient.AppSecret = "...";
    oAuthClient.RedirectUri = new Uri("https://.../....aspx");

    dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken(code);
    return tokenResult.access_token;

Now use (ver. 6):

    Dictionary<string, object> parameters = new Dictionary<string, object>();
    parameters.Add("client_id", "...");
    parameters.Add("redirect_uri", "https://.../....aspx");
    parameters.Add("client_secret", "...");
    parameters.Add("code", code);

    result = fb.Get("/oauth/access_token", parameters);

    string accessToken = result["access_token"];

(see: http://developers.facebook.com/docs/authentication/server-side/)

like image 30
Mateusz Avatar answered Sep 18 '22 01:09

Mateusz