13

I'm attempting to use the new Graph API Facebook recently released, but I can't seem to get it to work correctly.

I've gone through the steps, and after the /authorize call, I receive an access_token:

access_token=109002049121898|nhKwSTJVPbUZ5JYyIH3opCBQMf8.

When I attempt to use that token I get:

{
   "error": {
      "type": "QueryParseException",
      "message": "An active access token must be used to query information about the current user."
   }
}

I'm stumped as too why...

-AC

Kimtho6
  • 6,154
  • 9
  • 40
  • 56
Alex Cook
  • 171
  • 2
  • 2
  • 7
  • 1
    That `access_token` doesn't look complete to me. They are usually much longer than what you've shown. Maybe your code that is parsing the access_token from the URL is buggy? – E.M. May 11 '10 at 21:14

9 Answers9

12

When using your Facebook Application's token

If you're using the me alias as in https://graph.facebook.com/me/ but your token is acquired for a Facebook Application, then "me" isn't you anymore - it's the app or maybe nothing. Anyway, that's not your intention for the app to interact with itself.

In this case you will want to interact with your personal user account from an app. What you need to do (after giving the app the permissions it requests in the UI when it asks) is find your facebook userid # and put it in place of "me" to access your own info. e.g. Mark Zuckerberg's facebook userid is 4 so he is https://graph.facebook.com/4/

The alias me only works if you're you! Sometimes it's hard to remember who the current user is when programming facebook (i.e. you, the Page, the App, etc) because we're accustomed to using the facebook UI as ourselves most of the time. From a programming standpoint it depends on what the acquired token represents.

A great blog post that always helps correct me is Ben Biddington | Facebook Graph API — getting access tokens.

John K
  • 28,441
  • 31
  • 139
  • 229
  • 2
    Actually, it shouldn't. Getting info by id is a public method, and it's available even without correct user connected to FB - just try to call this method w/o auth - it'll return correct user info. Via passing "me" you're assuring that Facebook has identified you as FB user. – BasTaller Jul 10 '12 at 08:40
7

same thing here. I followed Ben Biddington's blog to get the access token. Same error when trying to use it. Facebook's OAuth implementation doesn't follow the spec completely, i am fine with it as long as the doc is clear, which obviously is not the case here. Aslo, it would be nice if the userid and username are returned with the access token.

JiJ
  • 526
  • 1
  • 5
  • 8
  • 3
    update, it looks like they fixed problem. Do NOT add type parameter. Aslo make sure redirect_uri is identical when making authroize and access_token call. – JiJ Apr 28 '10 at 15:27
  • Originally I want my own implemenation against their OAuth (and Twitter too) for control and performance, but began to have second thought. Different providers (e.g twitter and facebook) offer slightly different implementation, on top of that, oauth1/oauth2/ssl and new social features would requires a significant effort to keep up with changes and new stuff. For Facebook, just use their Social Gaph Javascript API, for twitter, just use @Anywhere api. Probably some overhead on page load, but should be acceptable in most cases. I can sleep better. – JiJ Apr 28 '10 at 15:29
  • 1
    up-voting for "make sure redirect_uri is identical when making authroize and access_token call" comment. this fixed my problem. finally can stop pulling out my hair. that "type=client_cred" only makes things worse. – nategood Mar 24 '11 at 00:53
  • Up vote on the redirect_uri being identical. Why this is no where else in the docs or examples is beyond me, but I just spent 4 hours debugging this and that's the only thing that works. – trumans1 Nov 07 '11 at 21:26
4

Just to clarify -- after you call

https://graph.facebook.com/oauth/authorize?

you should receive a CODE which, in conjunction with your CLIENT_ID and CLIENT_SECRET (assuming you have registered your application) can be exchanged for an access_token at

https://graph.facebook.com/oauth/access_token?

If this is indeed how you came by your ACCESS_TOKEN, you should then be able to request

https://graph.facebook.com/me/
Bosh
  • 8,138
  • 11
  • 51
  • 77
  • thats what he is asking, he is getting QueryParseException error from the server! – Cem Apr 24 '10 at 20:42
  • 1
    Correct, thats how I get the access_token - but when I try to hit https://graph.facebook.com/me/?access_token=foo I get the QueryParseException... Also, the access_token I'm getting doesn't have the &exp param, might have something to do with it... – Alex Cook Apr 24 '10 at 21:28
  • im struggling exactly the same thing here, if you find a solution please keep me posted. – Cem Apr 24 '10 at 22:23
  • I FINALLY got a different access_token, but it is still considered invalid by Facebook... – Alex Cook Apr 25 '10 at 15:04
  • is it url encoded the querystring you send? thats also needed, how did you manage to get a diff access_token? – Cem Apr 25 '10 at 16:41
  • Hi, even after removing type parameter, iam not getting the solution here... i get an error like: Invalid redirect_uri: Given URL is not allowed by the Application configuration. – shasi kanth Dec 28 '10 at 06:49
  • If you're using `https://graph.facebook.com/me/` but your token is for a **Facebook Application**, then "me" isn't you anymore - it's the app (and the app can't post a message to itself - that's not what you're intending to do either). In this case what you need to do is find your facebook userid # and put it in place of "me" to publish to your stream. e.g. Mark Zuckerberg's facebook userid is 4 so he is `https://graph.facebook.com/4/` – John K Jul 15 '11 at 01:57
2

Adding type parameter returns the auth_token for the application level, so it is better to OMIT it. What worked for me, after countless attempts and combinations, is using the same redirect_url parameter in the call to /oath/access_token as was used in the call to /oath/authorize.
So the full sequence to authorize your app on someone's behalf is:

1. call or redirect to:

"https://graph.facebook.com/oauth/authorize?client_id=" + my_clientId + "&scope=publish_stream,offline_access,manage_pages" + "&redirect_uri=" + "http://my_redirect_url?blah" 

2. in the page located at the return_url above, issue a request or what ever else to this url:

"https://graph.facebook.com/oauth/access_token?client_id=" + client_id + "&client_secret=" + secret + "&code=" + Request.QueryString["code"] + "&redirect_uri=" + "http://my_redirect_url?blah"
Shef
  • 44,808
  • 15
  • 79
  • 90
Fil
  • 21
  • 1
0

I've been having the exact same problem. A couple of things I've done to resolve it:

  1. Try it all out in the browser first to make sure the urls are correct at each stage
  2. Ensure the redirect url is identical, not just equivalent. Parameters in the same order, encoding the same
  3. Don't use the type=client_cred, or anything else for that matter
  4. Encode any ampersands in the redirect_url (but not the rest of the url) e.g. http://example.com/fb?foo=234%26bar=567. This one caused me the most issues. When the callback page was run, only the url before the first ampersand was included, as the ampersand was assumed to be part of the url for graph.facebook.com, not part of the redirect_url. I was then getting the values from the querystring to put in the redirect_url for the second call, but they weren't there. Once I encoded the ampersands they appeared correctly.
  5. Don't have any empty values in you encoded querystring parameters (e.g. ?foo=%26bar=123)
harriyott
  • 10,505
  • 10
  • 64
  • 103
0

I want to point out what has sort of been said on Ben Biddington's blog, and what I noticed from looking at the "malformed" access_token in the initial question. Others have said similar things in this thread, but I want to be explicit.

The token is not actually malformed, but rather a token that allows you to do actions on behalf of the APP, not the user. This is the token you'd use if you wanted to get all of the users of the app, or view insights for your app, etc, with the requests typically coming from your server, not the client. This type of token is gained by using the type=client_cred parameter. If you want to do things on behalf of the user, do not specify type=client_cred, and make sure you specify the following parameters in your call to http://graph.facebook.com/oauth/access_token:

'client_id' => APP_ID
'redirect_uri' => REDIRECT_URI
'client_secret' => APP_SECRET
'code' => $_GET['code']

I've written this as key-value pairs of a PHP array, but I think you get the point. The code GET value is gained after making the initial call to http://graph.facebook.com/oauth/authorize with the following parameters:

'client_id' => APP_ID
'redirect_uri' => "http://your.connect.url/some/endpoint"

I hope this helps! What the Facebook docs say, but don't say well, is that getting an access_token is a two-request process.

Andrew
  • 3,332
  • 4
  • 31
  • 37
0

I had the same issue in IE8 only. The solution for me was sending the access_token in the API request. Something like this:

FB.api('/me/friends?access_token=<YOUR TOKEN>

I obtained my token through PHP like this:

// Create our Application instance.
$facebook = new Facebook(array(
    'appId'  => '<API_ID>',
    'secret' => '<SECRET>',
    'cookie' => false,
));

$session = $facebook->getSession();
$token = $session['access_token'];
sakibmoon
  • 2,026
  • 3
  • 22
  • 32
0

I actually noticed that if your return uri doesn't have a slash on the end you have issues. I'm currently testing in the browser and return_uri=https://mydomain.com doesn't work but return_uri=https://mydomain.com/ does work. If I use the first I get "Error validating verification code."

This seems a bit odd, but I prolly just missed a word in the spec/instructions some where. Did lose two hours of my life to it though.

Grummle
  • 1,364
  • 1
  • 12
  • 21
0

I had the same problem, but getting rid of type=client_cred and making sure that the redirect_uri parameter is the same when making the authorize and the access_token call fixed the issue.

Sergi Mansilla
  • 12,495
  • 10
  • 39
  • 48