i can’t get an access token from facebook sdk

If I’ve understood the docs correctly, if you want to get an access token dynamically, according to https://developers.facebook.com/docs/facebook-login/web, you need enable fb-login for your app

  1. https://developers.facebook.com/apps/
  2. select your app
  3. add fb-login & follow the tips.

Otherwise you should just be able to get an access token for your app from the web interface and use that over and over.

See also : https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens

There’s also:

https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname&version=v9.0

Which enables you to generate an access token, and test out a call to the graph api, AND allows you to export code you can copy/paste:

enter image description here

try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb->get(
    '/me',
    '{access-token}'
  );
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphNode = $response->getGraphNode();

EDIT:

I see in your original question you state:

As I see the token is app id and app secret joined with pipe. “733163597544229|xxxxxxxxxxxxx9d14362xxxx3a6”. This should work but does not work.

According to the docs, if you want to supply an access token as {app_id}|{client-token} (not sure what secret you’re using here) you should append this to the URL, which is not in the URL you’re using.

See example here: https://developers.facebook.com/docs/facebook-login/access-tokens#clienttokens

The URL they use is:

curl -i -X GET "https://graph.facebook.com/{your-user-id}/accounts?access_token={user-access-token}

So for you this be:

$response = $fb->get('https://graph.instagram.com/me/media?fields=media_url,media_type&access_token=733163597544229|{client_token}');

EDIT 2:

I managed to get your sample working so perhaps these screen shots will help:

PHP Code:

<?php
$accessToken="EAAJIZAj**************************************************************************************************************************************************************************ZD";

require_once __DIR__ . '/Facebook/autoload.php'; // change path as needed

//echo __DIR__ . '/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
    'app_id' => '642**********502',
    'app_secret' => '0185*****************f743',
    'default_graph_version' => 'v2.10',
    'default_access_token' => "{$accessToken}"
]);

try {
    // Get the \Facebook\GraphNodes\GraphUser object for the current user.
    // If you provided a 'default_access_token', the '{access-token}' is optional.
    $response = $fb->get('/me');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();

?>

I took the source code from https://github.com/facebookarchive/php-graph-sdk and copied the src/Facebook dir into my project. I amended the path location in my code

require_once __DIR__ . '/Facebook/autoload.php'; // change path as needed

//echo __DIR__ . '/Facebook/autoload.php'; //<< comment out everything else and check the path is correct with this.

Working code: (same as above) with successful output shown at the bottom of the screen shot.

enter image description here

If you need to get the app secret go to the apps settings to uncover it:

enter image description here

Seems you were right – the access token does have an expiry and I had to refresh mine since using it yesterday.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top