2011-04-28 102 views
0

我正在遵循該教程。 http://developers.facebook.com/docs/authentication/臉譜圖API連接

但它重定向兩次。首先用代碼= NULL,下次它將有代碼..我不想重定向時代碼= NULL .plz告訴我的解決方案。這是我的完整代碼。

$code = $_REQUEST['code']; 
if(empty($code)) { 

    $dialog_url="https://www.facebook.com/dialog/oauth?client_id=$app_id&redirect_uri=$my_url&scope=email"; 
    echo("<script> top.location.href='" . $dialog_url . "'</script>"); 

} 
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" 
. $app_secret . "&code=" . $code; 



$access_token = file_get_contents($token_url); 

$graph_url = "https://graph.facebook.com/me?" . $access_token; 

$user = json_decode(file_get_contents($graph_url)); 
+0

如何來重定向你兩次?你的'$ my_url'和這個頁面一樣嗎? – ifaour 2011-04-28 20:13:29

回答

0
include_once 'config.php'; 

include_once 'lib.php'; 

$code = $_REQUEST["code"]; 
if(empty($code)) 
{ 
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state" 
    . $_SESSION['state']."&scope=email,user_birthday"; 

    $request_ids = $_REQUEST['request_ids'];//"&request_ids=".$request_ids 
    if(isset($_REQUEST['request_ids'])) 
    { 
      $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url."?request_ids=".$request_ids) . "&state" 
    . $_SESSION['state']."&scope=email,user_birthday"; 
    } 

echo("<script> top.location.href='" . $dialog_url . "'</script>"); 
} 
else 
{ 


    if(isset($_REQUEST['request_ids'])) 
    { 
     $theUrl = "http://apps.facebook.com/animathegame/index.php?request_ids=".$_REQUEST['request_ids']; 
    } 
    else 
    { 
     $theUrl = "http://apps.facebook.com/animathegame/index.php"; 
    } 

    $urls = "https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".$theUrl."&client_secret=".$app_secret."&code=".$code; 

    $token = file_get_contents($urls); 

    $sessionId = $token; 

    $urlss = "https://graph.facebook.com/me?".$token; 

    $thoken = file_get_contents($urlss); 



    $access_token_array = json_decode($thoken, true); 

    $userId = $access_token_array['id']; 
    $email = $access_token_array['email']; 
    $fullname = $access_token_array['name']; 
    $fname = $access_token_array['first_name']; 
    $lname = $access_token_array['last_name'];