Facebook login error: Could not open stream: HTTP request failed! HTTP / 1.0 400 Bad Request

I am trying to follow an example to create a server side login for Facebook from here , but no luck. In step 7, when I try to exchange code with a token and store it in a session for later use, I always get this error:

file_get_contents(): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

I know this has been asked many times, but I still can’t solve it, like this question , I tried using cURL, but it still does not work, it just returned false.

Another question told me to use the PHP SDK, but I do not know which method to use. Therefore, I am completely lost here.


Here is my code for calling the login form:

 <a href="<?php echo LOGIN_URL; ?> ?>" class="btn btn-primary">Login with Facebook</a> 

where LOGIN_URL is defined as:

 define("LOGIN_URL", $facebook->getLoginUrl(array("redirect_uri" => APP_URL . "fb_login"))); 

in fb_login, I have this code:

 $app->get("/fb_login", function() use($app, $facebook) { $code = $_REQUEST["code"]; $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . APP_ID . "&redirect_uri=" . APP_URL . $_SESSION["request_uri"] // Previous URL . "&client_secret=" . APP_SECRET . "&code=" . $code; $response = file_get_contents($token_url); // Doesn't work $params = null; parse_str($response, $params); $_SESSION["access_token"] = $params["access_token"]; // Let try with cURL... // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, $token_url); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // $output = curl_exec($ch); // Returns false // curl_close($ch); $graph_url = "https://graph.facebook.com/me?access_token=" . $params["access_token"]; $user = json_decode(file_get_contents($graph_url)); echo("Hello " . $user->name); var_dump($code); die(); }); 

Any help is appreciated. Thanks before.

+8
php facebook facebook-oauth oauth facebook-php-sdk
source share
2 answers

To answer the question, most likely the problem is not related to urlencode (URL safe) URL redirection. The fastest fix will be:

 $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . APP_ID . "&redirect_uri=" . urlencode(APP_URL . $_SESSION["request_uri"]) . "&client_secret=" . APP_SECRET . "&code=" . $code; 

However, I also suggest that you look at the Facebook SDK as this does all of this for you.

+6
source

You checked the documentation on the PHP SDK ... Its quite extensive and enjoyable. You just need to set up your Facebook SDK as follows

 $facebook = new Facebook(array( 'appId' => 'your app id', 'secret'=> 'your app secret')); 

Then you can get the login url as

 $loginURL = $facebook->loginUrl(array( 'scope' => 'read_stream', 'redirect_uri' => 'http://yourdomain/path/to/page')); 

And besides, the SDK can give you a more detailed description of your error, which it processes, which may be due to a mismatch in the redirect URI with application configurations or an error in the application identifier or a secret or completely different one. Download the SDK from here

+2
source

All Articles