yamcsha

Facebook - PHP - connection auth - SDK v3

Jul 16th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1. <?php
  2.  
  3.     // https://github.com/facebook/php-sdk/
  4.     require_once 'facebook-php-sdk/src/facebook.php';
  5.  
  6.     $app_id      = 'xxxxxxxxxxxxxxx';
  7.     $app_secret  = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  8.     $baseUrl     = 'http://www.example.lab/facebook/connect.php';
  9.  
  10.     session_start();
  11.     $code = $_REQUEST["code"];
  12.  
  13.     if(empty($code)) {
  14.         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
  15.         $dialog_url = "http://www.facebook.com/dialog/oauth?"
  16.                     . "client_id="      . $app_id
  17.                     . "&redirect_uri="  . urlencode($baseUrl)
  18.                     . "&state="         . $_SESSION['state']
  19.                     . "&scope=email,user_birthday";
  20.  
  21.         header('location:' . $dialog_url);
  22.         //echo("<script> top.location.href='" . $dialog_url . "'</script>");
  23.     }
  24.  
  25.     if (isset($_SESSION['state']) && ($_SESSION['state'] === $_REQUEST['state'])) {
  26.         $token_url = "https://graph.facebook.com/oauth/access_token?"
  27.                    . "client_id="       . $app_id
  28.                    . "&redirect_uri="   . urlencode($baseUrl)
  29.                    . "&client_secret="  . $app_secret
  30.                    . "&code="           . $code;
  31.  
  32.         $response  = file_get_contents($token_url);
  33.         $params    = null;
  34.         parse_str($response, $params);
  35.  
  36.         $graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token']
  37.                    . '&fields=id,email,first_name,last_name,gender,birthday,picture';
  38.  
  39.         $user = json_decode(file_get_contents($graph_url));
  40.         print '<img src='. $user->picture .' />';
  41.         var_dump($user);
  42.  
  43.     } else {
  44.         echo("The state does not match. You may be a victim of CSRF.");
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment