1

Very new to PHP and downloaded Facebook-PHP-SDK and I am trying to run a simple code I found online but I keep getting an error. I have done lots of research on the problem, looked at similar questions on here and so for but have not been able to find anything that helps. It looks like I still need to authorize the app I created in Facebook Developers, but I cannot figure out how to do that.

<?php
require_once(__DIR__ . '/vendor/autoload.php');
$fb = new Facebook\Facebook([
  'app_id' => '{}',
  'app_secret' => '{}',
  'persistent_data_handler' => 'memory',
  'default_graph_version' => 'v3.3',
  ]);

$helper = $fb->getCanvasHelper();

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)) {
  echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
  exit;
}

// Logged in
echo '<h3>Signed Request</h3>';
var_dump($helper->getSignedRequest());

echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

Error Message

No OAuth data could be obtained from the signed request. User has not authorized your app yet.
Tim
  • 191
  • 2
  • 28
  • 1
    See this, potential duplicate: https://stackoverflow.com/questions/40882664/error-to-get-the-access-token-in-facebook-using-php-sdk, it contains a link to https://developers.facebook.com/docs/facebook-login/ – Christian Aug 01 '19 at 22:01

2 Answers2

2

You are not supplying the SDK with the required app_id and app_secret so it can't do anything. That's why it gives you an OAUTH error. (fb-docs)

$fb = new Facebook\Facebook([
  'app_id' => '{}', // Here
  'app_secret' => '{}', // Here
  'persistent_data_handler' => 'memory',
  'default_graph_version' => 'v3.3',
  ]);

You need to get that data from your facebook app configuration dashboard. And plug into this object.

example:

$app_id = "1234";
$app_secret = "foobar";
$fb = new Facebook\Facebook([
  'app_id' => '{$app_id}',
  'app_secret' => '{$app_secret}',
  'default_graph_version' => 'v3.2',
  ]);
aviya.developer
  • 3,343
  • 2
  • 15
  • 41
  • I did that and I am still getting same error. Is there maybe something on my facebook developer account I need to do that I am not doing? – Tim Aug 01 '19 at 19:55
  • Did you authorize the app in the app dashboard? – aviya.developer Aug 01 '19 at 19:57
  • I became a verified admin then turned the status of my app to live. Is that what you mean? I think the problem could also be that my program is not getting an access token so it returns null for the access token which causes the program to crash. – Tim Aug 01 '19 at 20:02
  • Can you try and echo out the `$access_token` variable immediatly after it is set? to see what the result is? You didn't post and error message from the `catch` statements so it seems that something is working there, or at least no error is thrown. – aviya.developer Aug 01 '19 at 20:05
0

Apps are all registered and managed at https://developers.facebook.com/ . Have you checked your settings there?

Rajan
  • 15
  • 4