As I said in the comments of the question, you must utilise the same url (literally) of configuration Valid Oauth redirect uris when calling the method getLoginUrl
- If you use https at one URL, you should use at the other;
- If you nay use www in a URL, you nay should use in the other;
- If you use a file facebook-callback.php in one url, you must use in the other;
- Do not use similar URL, use the same.
If you use https://epcriacao.com/fb-callback.php in the method getLoginUrl
, you must use https://epcriacao.com/fb-callback.php in configuration URIs de redirecionamento do OAuth válidos
If you put a character more or less, will give error.
PHP:
session_start();
require "vendor/autoload.php";
$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
echo $helper->getLoginUrl("https://www.meu-site-de-exemplo.com/facebook-callback.php", ["email"]);
Setup:
![Configuração da URL de retorno no Facebook Developer](https://i.stack.imgur.com/Gt6MQ.jpg)
URL Generated:
https://www.facebook.com/v2.2/dialog/oauth?client_id=<CLIENT_ID>&state=<STATE>&response_type=code&sdk=php-sdk-5.6.1&redirect_uri=https%3A%2F%2Fwww.meu-site-de-exemplo.com%2Ffacebook-callback.php&scope=email
Generated URL Result:
![Resultado da URL Gerada](https://i.stack.imgur.com/F34mV.jpg)
facebook-callback.php
session_start();
require "vendor/autoload.php";
$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
Capturing the User Data:
$me = $fb->get("/me?fields=id,name,email", $accessToken);
$user = $me->getGraphUser();
echo $user["email"];
You’re probably not defining the same
url
redirect in the methodgetLoginUrl()
.– Valdeir Psr
And in which file is located this method so I can make this change?
– Eduardo Pereira
You have to post your code so that we can check where that fault is and check if that’s right.
– Valdeir Psr
I edited the question by adding the link with my files (I took a simple example that Facebook itself provides)
– Eduardo Pereira
In the archive
login.php
you must changehttps://example.com/fb-callback.php
to the URL you registered inURIs de redirecionamento do OAuth Válidos
– Valdeir Psr
In fact I had forgotten to change this file, I made the change but still persists in the error. If it’s not too much to ask, could you try doing something simple and test it yourself? Because I’m seeing several people with the same problem, so if someone more experienced test and work is our fault, otherwise I think it’s some problem with the same Facebook API...
– Eduardo Pereira
It is not SDK error, I tested now with your code (
login.php
) and it’s all OK. (I just added the same URL I signed up with Facebook Developer)– Valdeir Psr
Gee, but mine has the same 'https://epcriacao.com/fb-callback.php' URL... Could you send me a print link from your Basic Setup screen and the Facebook Login setup screen please? Thank you for your attention.
– Eduardo Pereira