EDIT: I have http://www.example.com
(example being my live site) as the Site URL under basic settings. I also have www.example.com
and example.com
under App Domains.
The error I'm getting is:
The redirect_uri URL must be absolute
My code breaks when I try to login. Here's my code for the getLoginUrl()
:
<?php
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$redirectURI = 'http://example.com/login-callback.php';
$encodedRedirectURI = urlencode($redirectURI);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl(
array(
'scope' => 'ads_management,read_insights',
'redirect_uri' => $encodedRedirectURI
)
);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
?>
Why is this error happening?
Because you are URL-encoding the URL, before passing it to the
getLoginUrl
method.The SDK takes care of that internally - so now you have a URL that has been URL-encoded twice, and that makes it unrecognizable as a valid, absolute URL.
So just pass the URL to that method without applying any extra encoding.
Edit: Additionally, the method
getLoginUrl
expects two parameters - first the redirect URI, and then the scope as the second one - not both as one array.You will also need to add your redirect URL (http://example.com/login-callback.php) as "Valid OAuth redirect URI" in the advanced settings of your facebook app (https://developers.facebook.com/apps).