2011-09-29 84 views
0

我試着去使用此代碼開始構建一個簡單的Facebook應用程序,但我不能似乎得到與訪問令牌部分交手,所以我可以得到用戶的生日等等試圖使用OAuth與Facebook的問題

燦有人請看看,讓我知道我在做什麼錯:

<?php 
$app_id = "*********"; 
$canvas_page = "https://apps.facebook.com/hotness-battle/"; 

$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . '&scope=email,user_birthday'; 

$signed_request = $_REQUEST["signed_request"]; 
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 

if (empty($data["user_id"])) { 
    echo("<script> top.location.href='" . $auth_url . "'</script>"); 
} else { 
    $token_url = 'https://graph.facebook.com/oauth/access_token?client_id=200482573356726&redirect_uri=http://www.impact25.com/hotness-battle/&client_secret=*******&code='.$data['oauth_token'].''; 
    echo("<script> top.location.href='" . $token_url . "'</script>"); 

    $uid = $data["user_id"]; 
    $token = $data['oauth_token']; 
    $full_name = json_decode(file_get_contents('http://graph.facebook.com/'.$uid))->name; 
    $gender = json_decode(file_get_contents('http://graph.facebook.com/'.$uid))->gender; 
    $birthday = json_decode(file_get_contents('http://graph.facebook.com/'.$uid.'?access_token='.$token))->birthday; 
    echo $full_name; 
    echo '<br><br>'; 
    echo $gender; 
    echo '<br><br>'; 
    echo $token; 
    echo '<br><br>'; 
    echo $cookie['access_token']; 
} 

回答

0

好了,明明是你剛纔複製從某處上面的代碼......這裏有幾個技巧:

  1. 閱讀Canvas Tutorial
  2. 不需要第二的OAuth請求($token_url),因爲如果用戶授權您的應用程序,你必須在signed_request
  3. access_token不要做多圖電話,一個電話將獲取你需要的一切
  4. 不打印access_token用戶
  5. 安全調用圖形(https

這裏是工作的代碼,讓你站電話:

<?php 
$app_id = "APP_ID"; 
$canvas_page = "https://apps.facebook.com/appnamespace"; 

$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . '&scope=email,user_birthday'; 

$signed_request = $_REQUEST["signed_request"]; 
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 

if (empty($data["user_id"])) { 
    echo("<script> top.location.href='" . $auth_url . "'</script>"); 
} else { 

    $uid = $data["user_id"]; 
    $token = $data['oauth_token']; 
    $graph_url = 'https://graph.facebook.com/' . $uid . '?access_token=' . $token; 

    $user_info = json_decode(file_get_contents($graph_url)); 
    $full_name = $user_info->name; 
    $gender = $user_info->gender; 
    $birthday = $user_info->birthday; 

    echo $full_name; 
    echo '<br><br>'; 
    echo $gender; 
    echo '<br><br>'; 
    echo $birthday; 
    echo '<br><br>'; 
}