I has been following the problems that returned by facebook sdk in my phalcon project, when I does facebook login: Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing. To resolve it, I consumed many times, but I didn't know reason of the problem.
Now I was located facebook-sdk-for-php-v5 in app/plugins/. My code is: class UserController extends ControllerBase { ... public function fb_init() { $fb = new FB(array( 'app_id' => '......', 'app_secret' => '..........', 'default_graph_version' => 'v2.5' ));
return $fb->getRedirectLoginHelper();
}
public function fb_loginAction()
{
$helper = $this->fb_init();
//$permissions = ['email']; Optional permissions
$loginUrl = $helper->getLoginUrl('https://153.121.73.26/in-movie/user/fb_callback');
foreach ($_SESSION as $k=>$v) {
if(strpos($k, "FBRLH_")!==FALSE)
if(setcookie($k, $v))
$_COOKIE[$k]=$v;
}
$this->response->redirect($loginUrl);
}
public function fb_callbackAction()
{
$helper = $this->fb_init();
try {
foreach ($_COOKIE as $k=>$v) {
if(strpos($k, "FBRLH_")!==FALSE)
$_SESSION[$k]=$v;
}
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$this->flash->error('Graph returned an error: ' . $e->getMessage());
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$this->flash->error('Facebook SDK returned an error: ' . $e->getMessage());
}
if (! isset($accessToken)) {
if ($helper->getError()) {
$errmsg = "Error: " . $helper->getError() . "<br/>";
$errmsg .= "Error Code: " . $helper->getErrorCode() . "<br/>";
$errmsg .= "Error Reason: " . $helper->getErrorReason() . "<br/>";
$errmsg .= "Error Description: " . $helper->getErrorDescription() . "<br/>";
} else {
$errmsg = 'Bad request';
}
$this->flash->error($errmsg);
}
else
$this->flash->success('Facebook login successfully!');
$this->response->redirect('user/index');
}
}
Well, I look forward to hearing from you asap. Thanks you.