Monday, June 18, 2012

Getting an App Access Token using the C# Facebook SDK

Getting an App Access Token using the C# Facebook SDK version 6.0

So there I was trying to figure out how to get the app access token using the C# Facebook SDK. I read about how to get one from Facebook's documentation, but it said something about extracting the value from the query string. But that didn't make too much sense. So I played around with some code and got it to work.


var fb = new Facebook.FacebookClient();

dynamic tokenInfo = 
  fb.Get(
    String.Format(
      "/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials", 
      appId, 
      appSecret));

var appAccessToken = (string)tokenInfo.access_token;


Yes, that simple. :)

Now with this app access token, I'm able to post on the each user's behalf if they granted me publish stream permissions. But what about if you want to post to a user's page? Well that is something that Facebook didn't make standard.

Here's what Facebook would like you to do.

1. Get a user access token
2. Extend that token to a 60-day token
3. Call /me/accounts and grab the access token from the page

This will be the non-expiring token you can use to publish with to the user's wall.


Happy coding!

9 comments:

  1. dear friend.. i tried this. but not working. i get an error.
    OAuthException - #2500) An active access token must be used to query information about the current user.

    ReplyDelete
    Replies
    1. Bro You Got Any For This Problem, Now I am Having this problem Please replay me.. @ emmadinaresh180@gmail.com

      Delete
  2. Hard to know where the issue lies since you have not told me what you're trying to do and where you're trying to do it. You can manually lint the access token at https://developers.facebook.com/tools/debug/access_token/ to see if it is still valid

    ReplyDelete
  3. Hey, I am trying to create a desktop application that gets facebook events from a facebook page, I have the page id and I can use the graph API, my problem lies in connecting the application to facebook, any help please?

    ReplyDelete
  4. Yeah, that's the fun part of a windows app. If you're writing a Windows 8 app, they have an example here: http://csharpsdk.org/docs/windows/

    Basically, you will need to launch the authentication via an IE browser control in your app, and be able to pull in the token from that broswer control.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I get Access token as 36xxxxxxxxx|nhjd1eq10yw7A3H9K_KzyK0BXzM

    IS this to be used directly to get or post something on wall?
    dynamic result1= objFacebookClient.Post("me/feed", new { message = "New " });
    OR
    dynamic result = client.Get("me/inbox", null);

    Both Gives me following error
    (OAuthException - #2500) An active access token must be used to query information about the current user.

    Am i missing something ?

    ReplyDelete
  7. Hi, your access token appears to be an "app" access token and not a "user" access token. Please read (http://dominicminicoopers.blogspot.com/2012/04/facebook-access-tokens-in-nutshell.html) for more information on the differences.

    ReplyDelete
  8. How in C# do you get a user access token and use it to post to the person using your app's news feed. Still found no working examples on the whole Internet that do this...

    ReplyDelete