0

I am developing a facebook application, so I need to do an http request to:

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN 

to get a string like this:

token=AAABz7kfdgXPwmgBANKZCctdZCPdfgfgcRJ0ONpQr8cWr336dfZDZD&expires=5168309

and then I need to retrieve the token:

AAABz7kfdgXPwmgBANKZCctdZCPdfgfgcRJ0ONpQr8cWr336dfZDZD

What do I have to use to do that ? curl library ? or is there a simplest way ?

OdinX
  • 4,135
  • 1
  • 24
  • 33
xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

0

You really shouldn't reinvent the wheel like that. Instead, use the existing PHP SDK:

Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • You're right, but it's a good practice to understand the facebook api logic. ^^ – Mike Boutin May 18 '12 at 17:37
  • For some people it might be a good idea to actually know how OAuth and other specifics of the Facebook API work, but in most cases, and I believe that includes xRobots', it's completely unnecessary – Jeroen May 18 '12 at 17:38
  • What does it mean ? "and I believe that includes xRobots" – Mike Boutin May 18 '12 at 17:40
  • in xRobots' (thats the OP) case ;) – Jeroen May 18 '12 at 18:49
  • I can't use the php sdk to retrieve the long-lived token: https://developers.facebook.com/roadmap/offline-access-removal/#extend_token – xRobot May 18 '12 at 22:17
  • 1
    Take a look at this: http://stackoverflow.com/questions/8982025/how-to-extend-access-token-validity-since-offline-access-deprecation – Jeroen May 18 '12 at 22:18
0

That's it !

function getAccessTok($code){
    if($code !== null){
        $urlTok = $urlGraphFB.'oauth/access_token';
        $urlTok .= '?client_id='.$appID;
        $urlTok .= '&redirect_uri='.$redirect_uri;
        $urlTok .= '&client_secret='.$_appSECRET;
        $urlTok .= '&code='.$code;

        $fileacctok = parse_str(file_get_contents($urlTok));
        return $fileacctok;
    }
}
Mike Boutin
  • 5,297
  • 12
  • 38
  • 65
  • If you want my custom facebook class i can give it to you. It's pretty easy to use, but can't do everything. You can use curl to replace the file_get_content... When i create it i didn't know curl yet. – Mike Boutin May 18 '12 at 17:32
  • what is the different from curl and file_get_content ? – xRobot May 18 '12 at 17:42