I followed the Facebook SDK for PHP docs and created two files, login.php and fb-callback.php, with all the lookup logic in fb-callback.php. When I do this, everything works fine.
But I want to move the lookup logic to get-posts.php and call it via ajax from fb-callback.php. When I do so, I can't seem to get the access token. I get the error noted below, "Access Token: Bad request".
I have registered both fb-config.php and get-posts.php as Valid OAuth Redirect URIs. So how do I get the proper parameters to get-posts.php?
Here are all the associated files:
login.php
<?php
require_once "config.php";
$redirectURL = 'https://' . $_SERVER[ 'SERVER_NAME' ] . '/r/fb-callback.php';
$permissions = ['email','user_photos','user_posts'];
$loginUrl = $helper->getLoginUrl($redirectURL, $permissions);
?>
<a href='<?php echo $loginUrl; ?>'>
<img src='continue-with-facebook.png'>
</a>
?>
config.php
<?php
if( !session_id() ) {
session_start();
}
require_once '/home/bitnami/vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '---',
'app_secret' => '---',
'default_graph_version' => 'v3.1',
]);
$helper = $fb->getRedirectLoginHelper();
?>
fb-callback.php
<?php
require_once("config.php");
?>
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
<script>
var $j = jQuery.noConflict();
$j(document).ready(function () {
$j.ajax({
type: "GET",
url: "get-posts.php",
cache: false,
success: function (html) {
setTimeout(function () {
$j('#updateDiv').html(html);
}, 1000);
}
});
});
</script>
</head>
<body>
<div id='updateDiv'><img src='spinning.gif' alt='processing...'></div>
</body>
</html>
get-posts.php
<?php
require_once("config.php");
// get the posts for this user id
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo __LINE__ . ' Access Token: Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo __LINE__ . ' Access Token: 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 __LINE__ . ' Access Token: Bad request';
}
exit;
}
...
Here is the actual code that works with explanations to what had to change below:
fb-callback.php
get-posts.php
So what changed? First, I needed to get the access code in fb-callback.php, not get-posts.php. So I moved the logic to get the access code and check for errors back to fb-callback.php. Then I added some logic to pass the access token collected to the parameter string of the AJAX url. (Look for "$get" in two places, once where I set the value and once where I attach it to the AJAX url.)
In get-posts.php, I removed the code that asks the Facebook API for an access token. Instead, I merely set the access token based on what was passed from fb-config.php via GET in the parameter named "accessToken" with the setDefaultAccesstoken() function from the Facebook PHP SDK.
As noted elsewhere, you can keep passing along the value of the access token via GET, REQUEST, or $_SESSION to page after page, so long as you maintain a session and inform Facebook via the setDefaultAccessToken() function at each subsequent page.