How to get FB access token given App ID and Secret?

amphibient picture amphibient · Mar 19, 2013 · Viewed 18.8k times · Source

I am just learning restfb. I registered an app on the FB site and got an app ID and Secret keys. However, as per this tutorial, I am supposed to have an access token to initialize DefaultFacebookClient. I tried to follow the instructions there but none sufficiently explained how to get the access token out of the app ID and secret key.

Request https://graph.facebook.com/oauth/authorize?client_id=MY_API_KEY&redirect_uri=http://www.facebook.com/connect/login_success.html

Facebook will redirect you to http://www.facebook.com/connect/login_success.html?code=MY_VERIFICATION_CODE

Request https://graph.facebook.com/oauth/access_token?client_id=MY_API_KEY& redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=MY_APP_SECRET&code=MY_VERIFICATION_CODE

Facebook will respond with access_token=MY_ACCESS_TOKEN

I did exactly that but every time I get

{
   "error": {
      "message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
      "type": "OAuthException",
      "code": 100
   }
}

Any idea what I am supposed to do to get an access token and start using the API?

Answer

cpilko picture cpilko · Mar 19, 2013

It looks like the plugin authors assume that you will be authenticating a user with the Login Dialog or the JavaScript SDK.

Both of these methods return a user access token you can use. You need your App_ID and Secret to initialize these. Most Facebook operations require a user access token.

If you are making API calls solely to read public data, then you may be able to get away with an application access token. For this you can use the DefaultFacebookClient().obtainAppAccessToken() method of RestFB.