2011-08-26 40 views
1

我曾經有過這個代碼發送的用戶正常應用驗收網頁,一旦他們來到我的申請頁面在Facebook上:不再要求用戶接受新用戶的申請嗎?

<?php 
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=email,user_photos,friends_photos"; 
$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 { 
    echo ("Welcome User: " . $data["user_id"]); 
} 
?> 

有了新的SDK我更換了所有與下面的是:

<?php 
require_once('src/facebook.php'); 
// Create our application instance 
// (replace this with your appId and secret). 
$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
)); 

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based 
// on whether the user is logged in. 
// If we have a $user id here, it means we know 
// the user is logged into 
// Facebook, but we don’t know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
try { 
// Proceed knowing you have a logged in user who's authenticated. 
$user_profile = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
error_log($e); 
$user = null; 
} 
} 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
$logoutUrl = $facebook->getLogoutUrl(); 

} else { 
$loginUrl = $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos')); 
} 
?> 

我的問題是我是否仍然需要正確頁面的頂部代碼來問這個用戶他們是否想授予我的應用程序訪問權限?只用最底層的代碼就不會再發生了嗎?Iis有一種更新的方法可以用新的SDK來完成嗎?

謝謝!

回答

0

讓我們先來修復代碼:

<?php 
require_once('src/facebook.php'); 
// Create our application instance 
// (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $app_secret, 
)); 
$canvas_page = "YOUR_URL_HERE"; 
// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based 
// on whether the user is logged in. 
// If we have a $user id here, it means we know 
// the user is logged into 
// Facebook, but we don’t know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
     echo '<pre>'.htmlspecialchars(print_r($user_profile, true)).'</pre>'; 
    } catch (FacebookApiException $e) { 
     error_log($e); 
     $user = null; 
    } 
} else { 
    echo "<script>top.location.href='" . $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos', 'redirect_uri'=>$canvas_page)) . "';</script>"; 
} 
?> 

注:

  • 你需要確保你使用$facebook->getUser()將檢查signed_request正確
  • 鏈接到SDK和另一來源爲當前用戶
  • getLoginUrl()將構建登錄U RL爲你
+0

啊啊非常感謝你ifaour! – Clint

相關問題