facebook图API连接

时间:2011-04-28 18:40:48

标签: facebook facebook-graph-api

我正在关注该教程。 http://developers.facebook.com/docs/authentication/

但重定向两次。首先使用code = 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));

1 个答案:

答案 0 :(得分: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'];