mdp716
mdp716

Reputation: 35

Facebook SDK v5 Graph returned an error: Invalid OAuth access token

Disclaimer: I am relatively new to php coding and very new to the Facebook SDK.

I have been trying to learn the Facebook API with their online materials and examples and find them to be rather lacking. I am trying to do something very basic - simply login with Facebook and retrieve a few user values.

Following Facebook's example I have login.php which looks like this:

<?php
session_start();

require_once __DIR__ . '/../_fb/autoload.php';

$fb = new Facebook\Facebook([
'app_id' => '26777overwritten335',
'app_secret' => '8260906deoverwrittenf286510',
'default_graph_version' => 'v2.6',
]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://www.domain.com/_mdp/fb_dev/login-callback.php', $permissions);

echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
?>

...and that seems to work fine.

On the login-callback.php page I have the following:

    <?php
session_start();

require_once __DIR__ . '/../_fb/autoload.php';

$fb = new Facebook\Facebook([
    'app_id' => '267772overwritten335',
    'app_secret' => '8260906de2overwritten1f286510',
    'default_graph_version' => 'v2.6',
]);

$helper = $fb->getRedirectLoginHelper();
try {
    $accessToken = $helper->getAccessToken();
} 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;
}

if (isset($accessToken)) {
    // Logged in!
    $_SESSION['facebook_access_token'] = (string) $accessToken;
    echo 'You are now logged in!';
    echo '<br> the access token is: ' . $accessToken . '<br>';

    // Now you can redirect to another page and use the
    // access token from $_SESSION['facebook_access_token']
}
// Am Successfully Logged In Here

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '$accessToken');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$user = $response->getGraphUser();

echo 'Name: ' . $user['name'];
// OR
// echo 'Name: ' . $user->getName();
?>

I do appear to be able to successfully login as I get the echo from the line:

echo 'You are now logged in!';

I have tried so many different examples of code and get various errors but the above code results in being echoed back the following:

Graph returned an error: Invalid OAuth access token.

And I have googled that every way I can think of and have yet to find out what I am doing wrong.

If anyone can show me where I am missing something and hopefully give me some corrected codelines it would certainly be appreciated.

Also, FYI - I really want to use graph version 2.6 with SDK 5 because it appears there are a few edges available that aren't in recent versions.

Thanks so much for any help pointing me in the right direction.

Upvotes: 3

Views: 7766

Answers (1)

C3roe
C3roe

Reputation: 96424

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '$accessToken');

You are passing the literal value $accessToken here - and that is of course not a valid access token.

PHP does not parse variables in strings with single quotes. Please go read http://php.net/manual/en/language.types.string.php in that regard, this is basic knowledge you need.

And then, stop putting simple variables into strings altogether.

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', $accessToken);

Upvotes: 5

Related Questions