开发者

FB PHP authentication

开发者 https://www.devze.com 2023-03-07 12:56 出处:网络
I found this code here http://developers.facebook.com/docs/authentication/ <?php $app_id = \"YOUR_APP_ID\";

I found this code here http://developers.facebook.com/docs/authentication/

<?php 

   $app_id = "YOUR_APP_ID";
   $app_secret = "YOUR_APP_SECRET";
   $my_url = "YOUR_URL";

   session_start();
   $code = $_REQUEST["code"];

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

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

   if($_REQUEST['state'] == $_SESSION['state']) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params[‘access_token’];

     $user = json_decode(file_get_contents($graph_url));
     echo("Hello " . $user->name);
   }
   else {
     echo("The state does not match. You may be a victim of CSRF.");
   开发者_高级运维}

?>

What I changed was the app id app secret and URL, I created a page with one HTML sentence I uploaded this on Web server and I get error page not found. Can anyone help me with that? Also do I need to include anything?


What you're trying to do in your code is kind of a workaround for something that already exists. Please use the Facebook PHP SDK for things that you're trying right now. It makes the thing a lot easier and you probably get to your goal in less time.


You need to use the facebook SDK. Here is the sample tutorial, which works me fine,

http://www.9lessons.info/2011/01/facebook-graph-api-connect-with-php-and.html

I am sure it will fix your issue.


You should use the Facebook PHP SDK (see on github). It is dead simple to deal with logging users in. Here is how it looks like :

require 'facebook.php';
$facebook = new Facebook(...);

$user = $facebook->getUser();

If the user is logged in, $user will contain his Facebook ID. But it does not mean that you have a valid access token to make API calls : if he just logged out from Facebook, $user will not be null but the access token will not be valid anymore.

One way to check if you have a valid token is actually to try to make a API call :

if ($user) {
  // The user is logged in
  try {
    $facebook->api('/me');
    // Here : API call succeeded, you have a valid access token
  } catch (FacebookApiException $e) {
    // Here : API call failed, you don't have a valid access token
    // you have to send him to $facebook->getLoginUrl()
    $user = null;
  }
} else {
  // The user is not logged
  // you have to send him to $facebook->getLoginUrl()
}

And then, to log in the user or to get a valid access token :

if (!$user) {
  // display link to $facebook->getLoginUrl();
}

You may want to check the example of the Facebook PHP SDK to have the complete flow, it is well documented.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号