Login to Facebook - PHP

Asked

Viewed 1,546 times

0

Colleagues, I am trying to enter the Facebook LOGIN option in my test application (Local Host), but is displaying the following error:

inserir a descrição da imagem aqui

My settings are as follows:

inserir a descrição da imagem aqui

and

inserir a descrição da imagem aqui

I’ve made several changes and so far nothing. Watching some videos on Youtube, lets understand that as most of them have been a while are outdated, so your settings are not the same.

Below is the link with the files I’m using: https://drive.google.com/drive/folders/1lpQy3bzPsGlZa9Ka0FOnsnrzT46gYewa

OBS.: I removed my 'app_id' and 'app_secret' just to be sharing my file, but the current one is filled correctly.

Could someone please help me?

  • You’re probably not defining the same url redirect in the method getLoginUrl().

  • And in which file is located this method so I can make this change?

  • You have to post your code so that we can check where that fault is and check if that’s right.

  • I edited the question by adding the link with my files (I took a simple example that Facebook itself provides)

  • In the archive login.php you must change https://example.com/fb-callback.php to the URL you registered in URIs de redirecionamento do OAuth Válidos

  • 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...

  • 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)

  • 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.

Show 3 more comments

1 answer

0


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

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

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"];

  • Got it! Actually I was typing wrong a character... However I made the fix and now it is returning me the following error message: Facebook SDK returned an error: Cross-site request forgery failed validation. Required param "state" Missing from persistent data.

  • Utilize session_start(); in the first line of the files login.php and fb-callback.php

  • Nothing either... I had researched about this error message and saw actually a post talking about it, but here it didn’t work. Did you use exactly the file I shared? Because it did not have session_start()

  • I didn’t use Session in the code because I didn’t get to test receiving "accessToken" because it wasn’t the error of the post. But I checked it now and it worked when I added session_start(); Ps.: In the documentation it has stating that CSRF is due. I updated my reply

  • Mine continues with the same error message. Could post the link from your files please?

  • https://mega.nz/#! Qp91xzsr! R8pkiquacjav-y-uADc8U9PtqDr08t75KzAh4tpi-2g

  • Oops, I tested my previous codes here and got it. In yours too, but using yours, in which variable I can get the login email ?

  • @Eduardopereira edited my answer. I added how to capture user data.

  • Now yes, you’re the man! Thank you so much for all your attention and dedication. Problem solved/

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.