0
votes

I'm trying to get the access token of my app using graph api, by requesting the following string:

https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=app_id&redirect_uri=canvas_page&scope=read_friendlists

This redirects to: canvas_page/#access_token=xxx&expires_in=4124&code=A...

But i am not sure how i can parse this with PHP, so that i eventually can make a json request to get the friendlist.

I hope you may be able to guide me :)

Thank you

3
You want to parse json result ?safarov

3 Answers

0
votes

Are you using the Facebook PHP sdk? https://github.com/facebook/php-sdk

You can use it like this:

$facebook = new Facebook(array(
  'appId'  => 'your_app_id',
  'secret' => 'your_app_secret',
));

// Get User ID
$user = $facebook->getUser();


if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
 } catch (FacebookApiException $e) {
     error_log($e);
    $user = null;
  }
}

if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

You can use the $loginUrl to make a link where the user needs to click that will authorize your application if they haven't done so already

Then you can make calls to the graph api like this:

$friends = $facebook->api('/user_id/friends');
0
votes

You can use the following code below. It worked for me.

First, you need to download Facebook SDK from here https://github.com/facebook/php-sdk

function facebook_oauth_init($facebook_app_id, $facebook_app_secret, $cookie = false)
{
    $facebook = new Facebook(array("appId" => $facebook_app_id, "secret" => $facebook_app_secret,
        "cookie" => $cookie));
    return $facebook;
}

$fb_app_id = "YOUR_APP_ID";
$fb_app_secret = "YOUR_APP_SECRET";
$fb_user_id = "YOUR_USER_ID";
$fb_access_token = "YOUR_GIVEN_ACCESS_TOKEN";

$facebook = facebook_oauth_init($fb_app_id, $fb_app_secret);
$accounts = $facebook->api('/' . $fb_user_id . '/accounts', 'get', array('access_token'=>$fb_access_token));
$accounts = $accounts['data'];

$access_token_2
foreach($accounts as $acc)
{
   if($fb_page_id == $acc['id'])
   {
       $access_token_2 = $acc['access_token'];
       break;
   }
}

"$access_token_2" should be what you are looking for.

Hope this helps.

Muhammad.

0
votes

Using this "api" method (on php sdk), you don't need to especify the access token, it will be generated and setted into Facebook object.

Anyway, you can to use $facebook->getAccessToken()

http://developers.facebook.com/docs/reference/php/facebook-getAccessToken/