0

I'm following the steps very well described here https://stackoverflow.com/a/18399927/2510225 , but, from my server, I receive the following error:

{"error":{"message":"The access token does not belong to application APP-ID","type":"OAuthException","code":1}}

I can't figure what I'm doing wrong. Anyone knows if the process to get a permanent access token has changed, or is having the same issue?

The access token I'm using in the request is the user access token, which I think is correct.

In other words, I'm using this:

GET /oauth/access_token?  
    grant_type=fb_exchange_token&           
    client_id={app-id}&
    client_secret={app-secret}&
    fb_exchange_token={short-lived-token}

With the app_id and app_secret of the app I want to publish on a page and the short-lived-token of the user that have created the app. Is that the right way?

Edition (Image to complement answer from @Sahil Mittal) That's where I'm taking the API_ID (red arrow). That's correct, right? enter image description here

Community
  • 1
  • 1
Leandro Guedes
  • 111
  • 1
  • 2
  • 14

2 Answers2

1

Ok, That's how I've solved this, combining both solutions given [here][1] with some tries. :

1) Associate the app with the page (It was probably done)

http://facebook.com/add.php?api_key=_APP_ID&pages=1&page=_PAGE_ID

2)Take the CODE given here:

https://graph.facebook.com/oauth/authorize?client_id=_APP_ID_&scope=manage_pages&redirect_uri=http://www.facebook.com/connect/login_success.html

There will be a very fast output on the URL box of your browser, copy that fast. This output should be like this

https://www.facebook.com/connect/login_success.html?code=1234546bigstringwithlotsoflettersandnumbersdfdarsd#_=_

3)Use the CODE to take the short lived access token of the USER (I guess it can be the same get with the Graph API Explorer))

https://graph.facebook.com/oauth/access_token?client_id=_APP_ID_&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=_APP_SECRET_&code=_CODE_

4)Convert the short lived access token to a long lived access token (user yet):

https://graph.facebook.com/oauth/access_token?client_id=_APP_ID_&client_secret=_APP_SECRET_&grant_type=fb_exchange_token&fb_exchange_token=_SHORT_LIFE_ACCESS_TOKEN_

You can check if this access token is long lived in https://developers.facebook.com/tools/debug/accesstoken

4) Go to Graph API Explorer (https://developers.facebook.com/tools/explorer), click on the "X" to clear the access token box, and fill in the long access token that you created in the previous step.

5) On the box bellow, choose /ACCOUNT/, to see all the pages of the user this access token is related with. The acess token of these pages are never expired access token, which can be verified in https://developers.facebook.com/tools/debug/accesstoken

That's how it worked for for me.

Leandro Guedes
  • 111
  • 1
  • 2
  • 14
  • 1
    Perfect, worked for me and saved me a lot of time with the facebook documentation! For the last step I did this instead to get a long lived access token for the page (from my long lived user access token) GET /{page-id}?fields=access_token – Thomas Jul 24 '16 at 11:10
0

You forgot to replace APP-ID with the relevant App ID.

You can get the same from the app settings


To get the short-lived-token:

if(empty($code))
{ 
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
                . "client_id=" . $APP_ID
                . "&redirect_uri=" . urlencode( $post_login_url)
                . "&scope=publish_stream,email";

    echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
else 
{
    $token_url = "https://graph.facebook.com/oauth/access_token?"
                    . "client_id=" . $APP_ID
                    . "&redirect_uri=" . urlencode( $post_login_url)
                    . "&client_secret=" . $APP_SECRET
                    . "&code=" . $_REQUEST["code"];

    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
}
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • Mitta Thank you very much. I added a picture to show you where I'm taking the APP-ID from. That's correct? – Leandro Guedes Nov 08 '13 at 22:19
  • Like this: **https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=123456789&client_secret=1234567865657656&fb_exchange_token=CRi2dzgiantstringIGRvcprLwZDZD** Just masked the IDs and cut part of the page access token (its really big) – Leandro Guedes Nov 09 '13 at 13:48
  • I added some more information to make the question more clear. – Leandro Guedes Nov 09 '13 at 15:17
  • What `short-lived-token` are you using in the url? (most prob this is the error) This token is the user's access token for that application; which is obtained from the login flow or it can also be obtained from the server side (ive added the code) – Sahil Mittal Nov 10 '13 at 04:55
  • thank you very much. I posted a road map of how I made this works. I'll put a +1 in your reputation as soon as I have reputation do to that. Thank you very much! – Leandro Guedes Nov 11 '13 at 19:44