Facebook过去几天是否随机更改了他们的API?我让我的网站与Facebook API完美配合,现在突然间它根本不起作用。不,我没有改变任何东西,它确实只是昨天决定不再重定向......它似乎只是尝试连接几次然后显示此页面:
“页面未正确重定向 Firefox检测到服务器正在以永远不会完成的方式重定向此地址的请求。 *此问题有时可能是由禁用或拒绝接受引起的 饼干“。
无论如何,这里有一些代码可以解决这个问题:P(是的,我确实拥有真正的应用程序ID并且已经到位) 这是fbLogin_member.php文件,您可以在点击登录链接后查找该文件:
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
if (empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url) ."&scope=email";
echo "<script> top.location.href='". $dialog_url ."'</script>";
}
$code = $_REQUEST['code'];
这是confirmlogin_fb_member.php文件:
//if user denies access to your website, take him to your manual login page
// if ($_GET['error']) {
// header("Location: memberlogin.php");
// exit;
// }
require_once('config/dbconfig.php');
$app_id = "my id #";
$app_secret = "my secret id";
$my_url = "http://www.sitedomain.com/confirmlogin_fb_member.php";
$code = $_GET['code'];
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
. $app_secret . "&code=" . $code;
// request access token
//use curl and not file_get_contents()
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$access_token = curl_exec($ch);
curl_close($ch);
$graph_url = "https://graph.facebook.com/me?" . $access_token;
// request user data using the access token
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$temp_user = curl_exec($ch);
curl_close($ch);
//decode the json array to get user data
$user = json_decode($temp_user);
//store user data
$u = $user->name;
$e = $user->email;
$fb_id = $user->id;
$username = $user->username;
$picture = 'https://graph.facebook.com/'. $fb_id .'/picture';
//check if user has already signed up before
$insert = true;
$result = mysql_query("SELECT * FROM members") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//if username already exists, do not insert
if (($row['name'] == $u) && ($row['userType'] == "facebook_user")) {
$insert = false;
}
}
// Random Password Generator
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
// end generator
// if new user, insert user details in your mysql table
if ($insert) {
mysql_query("INSERT INTO members(name, fb_username, password, email, profile_pic, userType) VALUES('$u', '$username', '$pass' , '$e', '$picture', 'facebook_user')") or die(mysql_error());
}
//login user
if (!session_start()) session_start();
$_SESSION['in'] = true;
$_SESSION['userName'] = $u;
$_SESSION['userType'] = "facebook_user";
$_SESSION['userEmail'] = $e;
//take user to his/her homepage
header("Location: layout.php");
最后,这是调用Facebook API会话的layout.php的顶部:
session_start();
require_once('config/dbconfig.php');
require_once('facebook/facebook.php');
if (isset($_REQUEST['logout'])) {
unset($_SESSION['in']);
unset($_SESSION['userName']);
unset($_SESSION['userType']);
unset($_SESSION['userEmail']);
session_destroy();
header("Location: layout.php");
}
$session = $_SESSION['in'];
if (!$session) {
$login = '<a href="fbLogin_member.php" class="fbLogin">Login with Facebook</a>';
$tooltipMsg = '<p>You must <strong>Log in</strong> to vote.</p>';
} else {
$sessionUser = $_SESSION['userName'];
$result = mysql_query("SELECT * FROM `members` WHERE name = '$sessionUser'") or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if ($result) {
$sessionRow = mysql_fetch_array($result);
$sessionUserid = $sessionRow['memberid'];
}
if ($sessionRow['userType'] == "facebook_user") {
$facebook = new Facebook(array(
'appId' => 'my app id #',
'secret' => 'my secret id',
'cookie' => true
));
// $session = $facebook->getSession();
$user = $facebook->getUser();
$me = null;
// Session based API call.
if ($user) {
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
// error_log($e);
}
}
}
我只是难以置信,它已经工作了好几个星期了,而现在当我从一天半的时间回家后它无法正常工作。任何帮助都会受到赞赏,即使你的编码看到其他一些错误(这是我第一次尝试使用facebook api):)
答案 0 :(得分:0)
听起来你可能有无限重定向问题?
您正在检查是否在分配$ code之前设置了$ code,尝试翻转这两个语句的顺序:
$ code = $ _REQUEST ['code'];
if(empty($ code)){ $ dialog_url =“http://www.facebook.com/dialog/oauth?client_id=”。 $ app_id。“&amp; redirect_uri =”。 urlencode($ my_url)。“&amp; scope = email”; echo“top.location.href ='”。 $ dialog_url。“'”; }
答案 1 :(得分:0)
你可能需要在base_facebook.php中的受保护函数getCode()中将$ _REQUEST更改为$ _GET
protected function getCode() {
if (isset($_GET['code'])) {
if ($this->state !== null &&
isset($_GET['state']) &&
$this->state === $_GET['state']) {
// CSRF state has done its job, so clear it
$this->state = null;
$this->clearPersistentData('state');
return $_GET['code'];
} else {
self::errorLog('CSRF state token does not match one provided.');
return false;
}
}
return false;
}