Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook SDK returned an error: Cross-site request forgery validation failed. The "state" param from the URL and session do not match

i'm trying to get Facebook user id using the php sdk like this

$fb = new Facebook\Facebook([     'app_id' => '11111111111',     'app_secret' => '1111222211111112222',     'default_graph_version' => 'v2.4', ]);  $helper = $fb->getRedirectLoginHelper();   $permissions = ['public_profile','email']; // Optional permissions $loginUrl = $helper->getLoginUrl('http://MyWebSite', $permissions);  echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';       try {         $accessToken = $helper->getAccessToken();         var_dump($accessToken);     } catch (Facebook\Exceptions\FacebookResponseException $e) {         // When Graph returns an error         echo 'Graph returned an error: ' . $e->getMessage();         exit;     } catch (Facebook\Exceptions\FacebookSDKException $e) {         // When validation fails or other local issues         echo '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 'Bad request';         }         exit;     }  // Logged in     echo '<h3>Access Token</h3>';     var_dump($accessToken->getValue());  // The OAuth 2.0 client handler helps us manage access tokens     $oAuth2Client = $fb->getOAuth2Client();  // Get the access token metadata from /debug_token     $tokenMetadata = $oAuth2Client->debugToken($accessToken);     echo '<h3>Metadata</h3>';     var_dump($tokenMetadata);  // Validation (these will throw FacebookSDKException's when they fail)     $tokenMetadata->validateAppId($config['11111111111']); // If you know the user ID this access token belongs to, you can validate it here //$tokenMetadata->validateUserId('123');     $tokenMetadata->validateExpiration();      if (!$accessToken->isLongLived()) {         // Exchanges a short-lived access token for a long-lived one         try {             $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);         } catch (Facebook\Exceptions\FacebookSDKException $e) {             echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";             exit;         }          echo '<h3>Long-lived</h3>';         var_dump($accessToken->getValue());     }      $_SESSION['fb_access_token'] = (string)$accessToken; 

but it give me this error:

Facebook SDK returned an error:  Cross-site request forgery validation failed.  The "state" param from the URL and session do not match. 

please any help i'm new in php and Facebook sdk's thank for any help in advance.

like image 493
Fadi Avatar asked Aug 15 '15 20:08

Fadi


2 Answers

insert this code after $helper = $fb->getRedirectLoginHelper();

  $_SESSION['FBRLH_state']=$_GET['state']; 
like image 44
zratan Avatar answered Sep 18 '22 14:09

zratan


I found that as long as I enabled PHP sessions before generating the login url, and at the top of the script Facebook eventually redirects to, it works just fine on its own without setting a cookie (as per ale500's answer). This is using the 5.1 version of the sdk.

At the top of both scripts, I added...

if(!session_id()) {     session_start(); } 

...and it "just worked".

Here's a barebones complete example that worked for me:

auth.php

if (!session_id()) {     session_start(); }  $oFB = new Facebook\Facebook([     'app_id'     => FACEBOOK_APP_ID,     'app_secret' => FACEBOOK_APP_SECRET ]);  $oHelper = self::$oFB->getRedirectLoginHelper(); $sURL = $oHelper->getLoginUrl(FACEBOOK_AUTH_CALLBACK, FACEBOOK_PERMISSIONS);  // Redirect or show link to user. 

auth_callback.php

if (!session_id()) {     session_start(); }  $oFB = new Facebook\Facebook([     'app_id'     => FACEBOOK_APP_ID,     'app_secret' => FACEBOOK_APP_SECRET ]);  $oHelper = self::$oFB->getRedirectLoginHelper(); $oAccessToken = $oHelper->getAccessToken(); if ($oAccessToken !== null) {     $oResponse = self::$oFB->get('/me?fields=id,name,email', $oAccessToken);     print_r($oResponse->getGraphUser()); } 

Why?

As an additional note, this is explained in the Docs on the repo. Look at the warning on this page.

Warning: The FacebookRedirectLoginHelper makes use of sessions to store a CSRF value. You need to make sure you have sessions enabled before invoking the getLoginUrl() method. This is usually done automatically in most web frameworks, but if you're not using a web framework you can add session_start(); to the top of your login.php & login-callback.php scripts. You can overwrite the default session handling - see extensibility points below.

I'm adding this note because it's important to keep in mind should you happen to be running your own session management or if you're running multiple web servers in parallel. In those cases, relying upon php's default session methods won't always work.

like image 133
enobrev Avatar answered Sep 17 '22 14:09

enobrev