2

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?

amphibient
  • 29,770
  • 54
  • 146
  • 240

3 Answers3

3

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.

cpilko
  • 11,792
  • 2
  • 31
  • 45
  • 1
    I am actually just trying to programmatically access my friend list – amphibient Mar 19 '13 at 02:11
  • You need a user access token for that. If you want to play with the API without having to worry about authentication, grab one from the [Graph API Explorer](https://developers.facebook.com/tools/explorer). – cpilko Mar 19 '13 at 02:17
  • i just made some updates. what do you mean by "play with the API without having to worry about authentication"? – amphibient Mar 19 '13 at 02:22
  • Their tutorial is wrong. The ["Login for Server-Side Apps" documentation](http://developers.facebook.com/docs/howtos/login/server-side-login/) gives a PHP-centric idea of what you need to do to authenticate. Getting Facebook authentication working is frustrating, and easier done on the client-side. If you are in the testing phase, and don't want to code and troubleshoot the whole user authentication workflow, you can get a valid user access token for you from the Graph API explorer, and paste this into your code. – cpilko Mar 19 '13 at 02:35
  • how can i "get a valid user access token for you from the Graph API explorer"? – amphibient Mar 19 '13 at 03:08
  • 1
    Visit https://developers.facebook.com/tools/explorer. Click the "Get Access Token" button. Copy the contents of the "Access Token" field. – cpilko Mar 19 '13 at 03:10
  • BTW, do you have any idea why their API is called "Graph"? I don't see how it has anything to do with graphs – amphibient Mar 19 '13 at 04:02
  • I think it has to do with access to the social graph that you traverse with Facebook. – cpilko Mar 19 '13 at 13:56
1

I were having the same issue.

Adding a trailing slash to the redirect_uri-parameter solved the problem for me.

Also, make sure you are using the URI for your own app and not a facebook URI when making the requests.

E.g:

http://www.yoursite.com

didn't work for me while

http://www.yoursite.com/

did.

Erik Wendel
  • 91
  • 2
  • 9
1

I am not sure if that's what you want, but anyway:

To get access token for users associated with an app, you can use the /accounts 'edge' (see here).

Also, this site about access tokens says that you can make calls to Graph API like that: https://graph.facebook.com/endpoint?access_token=app_id|app_secret

Now, to combine that, to get the access tokens (in JSON format along with some other stuff): https://graph.facebook.com/{your app id here}/accounts?access_token={again, your app id here}|{your app secret here}

Please substitute the fields in the url above and remove all the { and } from it.

ed22
  • 1,127
  • 2
  • 14
  • 30