我正在尝试 facebooks 服务器端登录(使用 PHP),除了显示我的名字之外,一切似乎都正常。所以,我一直在遵循本指南:https://developers.facebook.com/docs/howtos/login/server-side-login/一切都和上面写的一样。我已经检查并删除了所有内容两次以确保其正确。因此,当我登录我的网站时,有时会收到“状态不匹配。您可能是 CSRF 的受害者。”,但随后我总是收到 Hello。 “你好”后面应该是我的名字,但事实并非如此。所以很明显登录和授权部分有效,但它不会显示我的名字。你们中有人知道可能出了什么问题吗?
这是我的代码:
<?php
$app_id = "MY_APP_ID";
$app_secret = "MY_APP_SECRET";
$my_url = "MY_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=user_birthday,read_stream";
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['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);
$_SESSION['access_token'] = $params['access_token'];
$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.");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Personal testing site</title>
</head>
<body>
<p>This is a personal testing site, and is not related to *mysite* or any of their upcoming functions/ products. </p>
</body>
</html>
编辑1:
我想我可能已经找到了错误所在,但我仍然不知道如何修复它。因此,当我编写代码并尝试回显代码的特定部分时,我发现 access_token 为空。所以, $token_url 有效(我认为),但是当我尝试回显 $response 或 $_SESSION['access_token'] 时,它们都是空白的。当我尝试回显 $graph_url 时,我得到“https://graph.facebook.com/me?access_token= ”, token= 后面没有任何内容,而 access_token 应该连接在此处。我不知道是否是串联问题,或者访问 token 是否为空,但我认为这是最后一个。我一遍又一遍地检查我的代码,当我将它与 Facebook 网页上的代码进行比较时,没有任何问题。
你们认为你们能找到什么吗?
编辑2:
当我回显 $token_url 并将其输入到我的网络浏览器中时,我收到此错误消息:
{
"error": {
"message": "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request",
"type": "OAuthException",
"code": 100
}
}
你们认为这可能与 facebook 使用 https 运行,但我的网站只使用 http 的事实有关吗?
编辑3:
这是我的应用程序设置的图片: *已删除_图片*
编辑4:
好的,我已经解决了。问题是应用程序设置中的 url 必须最后一个/,并且我的代码中的 $my_url 必须与其完全相同。
但是,我又遇到了一个问题。看起来像 Æ Ø Å 这样的字母在某个地方被扭曲了,Ø(我的姓氏)变成了 (¸)(不带括号)。有谁知道如何解决这个问题吗?
最佳答案
您的应用设置似乎存在一些问题。只需输入您的网站网址 - http://testing123.feedlobby.com/在“带有 FB 登录网址的网站 - 网站 URL”字段中。将“应用程序域”字段留空。我建议创建一个新应用程序并以这种方式进行设置,然后使用您的新应用程序 ID 和密码。
此外,尝试将 echo 语句包含在 html 正文中。
关于php - 使用 Facebook 服务器端登录让用户登录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964925/