Simple Iframe based facebook authentication

1.7k views Asked by At

I wrote a facebook application but authentication is not working properly. Despite of googling several hours it did not solve. please please write the script which authenticates the user properly and if the user clicks the allow button simply display the text "Welcome" assuming that i have written all basic suff aas..

require 'sdk/src/facebook.php';
 $app_id = 'MY APP ID';
 $canvas_page = "MY CANVAS URL";

// Create our Application instance (replace this with your appId and secret).
   $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
            . $app_id . "&redirect_uri=" . urlencode($canvas_page) ."&scope=email,read_stream&installed=1";
  $facebook = new Facebook(array(
  'appId'  => $app_id ,
  'secret' => MY APP SECRET,
  'cookie' => true,
));
///... your code to solve !
2

There are 2 answers

0
Shahid Karimi On BEST ANSWER

Nobody provides the ultimate solution: Finally I solved it by myself. what I do is. Facebook sends an "installed=1" parameter with the url when the user allows the application. SO I checked this parameter and redirected again to the application. I m extremely happy on finding extremely nice and efficient solution. Nobody answered me any votable answer.

if(isset($_REQUEST['installed']))
  echo("<script>top.href.location='http://apps.facebook/myapp' </script>");
4
ifaour On

It seems that you are mixing the examples from the documentations and the PHP-SDK, dropping the example from the PHP-SDK should work.

Following the code in this answer:

<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'XXXXXXX',
  'secret' => 'XXXXXXXX',
  'cookie' => true,
));

$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl();
$me = null;

if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');

    echo "Welcome User: " . $me['name'] . "<br />";
  } catch (FacebookApiException $e) {
    error_log($e);
  }
} else {
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>