FB PHP 认证
我在这里找到了这段代码 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试在代码中执行的操作是对已经存在的内容的一种解决方法。对于您现在正在尝试的操作,请使用 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.
您需要使用 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.
您应该使用 Facebook PHP SDK(参见 github)。处理用户登录非常简单。如下所示:
如果用户登录,
$user
将包含他的 Facebook ID。但这并不意味着您拥有有效的访问令牌来进行 API 调用:如果他刚刚从 Facebook 注销,$user
将不为 null,但访问令牌将不再有效。检查您是否拥有有效令牌的一种方法实际上是尝试进行 API 调用:
然后登录用户或获取有效的访问令牌:
您可能需要检查 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 :
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 :
And then, to log in the user or to get a valid access token :
You may want to check the example of the Facebook PHP SDK to have the complete flow, it is well documented.
Hope that helps.