FB PHP 认证

发布于 2024-11-09 06:35:39 字数 1450 浏览 0 评论 0原文

我在这里找到了这段代码 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.");
   }

?>

什么我更改了应用程序 ID 应用程序密码和 URL,我创建了一个包含一个 HTML 句子的页面,我将其上传到 Web 服务器上,但出现错误页面未找到。有人能帮我吗?我还需要包含什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-11-16 06:35:39

您尝试在代码中执行的操作是对已经存在的内容的一种解决方法。对于您现在正在尝试的操作,请使用 Facebook PHP SDK。它使事情变得容易得多,您可能会在更短的时间内实现目标。

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.

信愁 2024-11-16 06:35:39

您需要使用 facebook SDK。
这是示例教程,对我来说效果很好,

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

我相信它会解决您的问题。

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.

娇俏 2024-11-16 06:35:39

您应该使用 Facebook PHP SDK(参见 github)。处理用户登录非常简单。如下所示:

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

$user = $facebook->getUser();

如果用户登录,$user 将包含他的 Facebook ID。但这并不意味着您拥有有效的访问令牌来进行 API 调用:如果他刚刚从 Facebook 注销,$user 将不为 null,但访问令牌将不再有效。

检查您是否拥有有效令牌的一种方法实际上是尝试进行 API 调用:

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()
}

然后登录用户或获取有效的访问令牌:

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

您可能需要检查 Facebook PHP SDK 的示例 拥有完整的流程,有详细的文档记录。

希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文