Setting up Facebook Login on localhost

MattD picture MattD · Feb 2, 2013 · Viewed 17.1k times · Source

I've been following the steps outlined here to create a Facebook App Login: http://developers.facebook.com/docs/howtos/login/server-side-login/

And am on Step 3. I'm setting this up on localhost for development and then I'll publish to Azure Websites with a different App ID. I'd like to have this working on localhost first though. (I haven't tried this with a non-localhost domain yet.)

On my Facebook App configuration page I've set a couple relevant fields: Site URL: http:// localhost : 8052/ (Spaces added to allow submision to Stack Overflow) App Domains: localhost

main.php redirects to a login page if there is no Facebook code set. This works fine.

<head>
<?php
   if(!isset($_SESSION['code'])) {
      echo ('<meta http-equiv="REFRESH" content="0;url=/login.php" />');
   }
?>
</head>

login.php redirects to Facebook's login page using code copied from their example.

<?php

   include '/lib/url.php';

   $app_id = "XXXXX";
   $app_secret = "XXXXX";
   $my_url = CreateUrlForPage('welcome.php');

   session_start();

   $code = '';
   if(!isset($_REQUEST["code"])) {
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'];

     echo $my_url.'<br/>';
     echo $dialog_url.'<br/>';
     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }
?>  

Clearly I've obfuscated my App ID above.

The URL generated by my "CreateUrlForPage" function looks fine as does the facebook login URL:

http://localhost:8052/welcome.php
https://www.facebook.com/dialog/oauth?client_id=XXXX&redirect_uri=http%3A%2F%2Flocalhost%3A8052%2Fwelcome.php&state=XXXX

(Again, some obfuscation here. The real values looked okay. The first being an integer the second being a long hex string.)

When I navigate to http:// localhost : 8052/login.php, The page redirects to Facebook and gives a completely useless error: "An error occurred. Please try again later."

I looked at a couple other posts on Stack Overflow, but they didn't solve my problem.

The error I'm getting looks just like the one described here: Facebook application login error on localhost The suggestion provided is to set the Domain and Site URL, which I've done.

There is also a link to this question: Running Facebook application on localhost Again, they suggest Site URL. They also talk about app-canvas which is not something I'm using and I don't see how it would be related to login.

For what it's worth, before I set the Site URL and Domain, I was getting a different error - one that told me I needed to set the Site URL.

Answer

rana picture rana · Feb 3, 2013

this code worked for me. double check your facebook app id and my site url is set to http:// localhost:8052/ dont really need to set domain name for login thanks

http://gamma.owlweb.com.ua/index.php/?route=account/register/fb

<?php

$app_id = "xxx";    $app_secret = "xxx"; 
$my_url ="http://localhost:3080/abc.php";

session_start();

$code = '';    

if(!isset($_REQUEST["code"])) {
  $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
  $dialog_url = 
    "https://www.facebook.com/dialog/oauth?client_id=".$app_id
    ."&redirect_uri=".urlencode($my_url)
    ."&state=".$_SESSION['state'];

  echo $my_url.'<br/>';
  echo $dialog_url.'<br/>';
  echo("<script> top.location.href='" . $dialog_url . "'</script>");
} 
?>