2011-07-03 33 views
2

Facebook在过去几天只是随机更改他们的API?我让我的网站与Facebook API完美协作,现在突然间它根本不起作用。不,我没有改变任何东西,它只是决定昨天不再重定向...它似乎只是尝试连接几次,然后它显示此页面:Facebook连接没有正确重定向所有的突然

“页面没有正确重定向 Firefox已检测到服务器正在以永不完整的方式重定向此地址的请求。 *此问题有时可能由禁用或拒绝接受 Cookie引起。

无论如何,这里有一些代码来包装你的头: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

的API昨天改了改$ _REQUEST到$ _GET的保护功能引用代码()...更多信息@ HTTPS://developers.facebook .com/blog /(你应该订阅)。 – Mike

+0

为什么你不能使用 ..? – Shameer

+1

啊,去图...谢谢!将重新修复一遍: – Nate

回答

0

这听起来像你可能有一个无限的重定向问题?

要检查,看看是否$代码分配给它之前设置,尝试翻转这两个语句的顺序:

$代码= $ _REQUEST [“代码”];

if(empty($ code)){{}} {dialog_url =“http://www.facebook.com/dialog/oauth?client_id=”。 $ app_id。“& redirect_uri =”。 urlencode($ my_url)。“& scope = email”; echo“top.location.href ='”。 $ dialog_url。“'”; }

0

您可能需要在base_facebook.php

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; 
    }