Search code examples
phpfacebookfacebook-php-sdk

What is the access token in Facebook PHP SDK?


Very new to PHP and I trying to write a program using Facebook PHP SDK and right now I am trying to get the following tutorial to work, https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile, but I keep getting an error. I think it might have to do with the access token. I do not know if I am suppose to put something else there or not. And if I am how do I get my access token? Right now I just left it the way it was in the tutorial. But the error is saying something is wrong with line 2, so maybe the problem is there. Not quite sure. So any help would be greatly appreciated!

Error Message

[north@oreo ~/Facebook]$ php Practice_2.php

Fatal error: Uncaught Error: Class 'Facebook\Facebook' not found in /usr/home/north/Facebook/Practice_2.php:2
Stack trace:
#0 {main}
  thrown in /usr/home/north/Facebook/Practice_2.php on line 2

Practice_2.php

<?php
$fb = new Facebook\Facebook([
  'app_id' => '{}',
  'app_secret' => '{}',
  'default_graph_version' => 'v3.3',
  ]);

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', '{access-token}');
} 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();

Solution

  • From the docs

    When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.