2011-07-03 61 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; 
    }