0

I have 2 nights without sleep trying to solve this problem but today I feel defeated and I come to you for help.

I'm on MacOS with High Sierra, on localhost with MAMP.

index.php

<?php
session_start();

require_once 'vendor/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'APP_ID',
  'app_secret' => 'APP_SECRET',
  'default_graph_version' => 'v2.10',
  ]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://localhost/facebookphp/fb-callback.php', $permissions);

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

?>

fb-callback.php

<?php
session_start();

require_once 'vendor/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'APP_ID',
  'app_secret' => 'APP_SECRET',
  'default_graph_version' => 'v2.10',
  ]);

$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)) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    echo 'Bad request';
  }
  exit;
}

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

// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('APP_ID'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();

if (! $accessToken->isLongLived()) {
  // Exchanges a short-lived access token for a long-lived one
  try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
  }

  echo '<h3>Long-lived</h3>';
  var_dump($accessToken->getValue());
}

$_SESSION['fb_access_token'] = (string) $accessToken;



// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
// header('Location: http://localhost/members.php');

Screenshots of the configuration on Facebook:

enter image description here

enter image description here

Error: Graph returned an error: The URL can not be loaded: The domain of this URL is not included in the application domains. In order to load this URL, add all domains and subdomains of your application in the "Application Domains" field in the application settings.

Edwin
  • 1,135
  • 2
  • 16
  • 24
GePraxa
  • 67
  • 1
  • 17
  • Facebook seems not accept localhost callback redirect? Try to use [ngrok](https://ngrok.com/) to get you localhost server go public – Stanley Cheung Oct 19 '17 at 06:09
  • Check https://stackoverflow.com/questions/4532721/facebook-development-in-localhost/7493806#7493806 for more example – Stanley Cheung Oct 19 '17 at 06:10
  • i hope you delete this app after you are finished testing :) – madalinivascu Oct 19 '17 at 06:12
  • Possible duplicate of [Facebook development in localhost](https://stackoverflow.com/questions/4532721/facebook-development-in-localhost) – Edwin Oct 19 '17 at 06:36
  • @StanleyCheung Thanks for the information, I'll try this method. – GePraxa Oct 19 '17 at 06:37
  • @StanleyCheung Thank you, I will try all the methods on the page that you give me, tomorrow I will return with the news, I hope to solve this. – GePraxa Oct 19 '17 at 06:38
  • @madalinivascu He is haha, in fact already erased, only believes it to be able to publish this question, the real one is locked ;) – GePraxa Oct 19 '17 at 06:39
  • @da39a3ee I know, there are many questions on this subject, but none has the answer that can help me, an apology for duplicate. Regards! – GePraxa Oct 19 '17 at 06:41
  • Could you edit your post and describe why those answers are not working for you? It looks like the same question to us. :-) – Edwin Oct 19 '17 at 06:47
  • Nothing works, I'm even testing on a real server with a real domain and the problem is the same. Any ideas? – GePraxa Oct 19 '17 at 07:58
  • Something seems to be massively buggy with this over the last couple days, you see people reporting this all over the place, with configurations that should work. Go search for "app domains" on https://developers.facebook.com/bugs/, and subscribe to one of the existing recent bug reports on the issue. If they say it should be resolved, tell them that it still isn't for you. – CBroe Oct 19 '17 at 11:16
  • @CBroe Thank you!!!!! This makes me feel less awkward, with JS working perfectly everything but with PHP something seems to be broken, now I understand that I am not the only one in this gap. Thanks again! – GePraxa Oct 19 '17 at 23:53

1 Answers1

1

Try modify this in your fb-callback.php file

replace

$accessToken = $helper->getAccessToken();

for

$accessToken = $helper->getAccessToken("http://localhost/facebookphp/fb-callback.php");

Add the same Valid OAuth Redirect URIs field in method getAccessToken

Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47