2011-10-02 50 views
1

evertything正在運行在我的Facebook應用程序正常,直到我把它升級到的OAuth 2.0,和IM不確定是不是我做的一切權利。故障使用OAuth 2.0在我的Facebook應用程序

的事情是,我已經取得的OAuth的對話工作,並在用戶授權的應用程序,它使我的應用程序到iFrame,但我有我的$ _GETs麻煩[],讓我解釋一下:

這裏是我的index.php,這是我的主網頁使用,而我只是包括()在一個div一些文件調用的內容,這取決於$ _GET [「節」]:

$app_id = "xxx"; 
$application_secret = 'xxx'; 
    $canvas_page = "xxx"; 
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id=".$app_id. "&redirect_uri=".urlencode($canvas_page)."&scope=publish_stream,offline_access"; 

//OAuth 2.0 

    function parse_signed_request($signed_request, $secret) { 

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     // decode the data 
     $sig = base64_url_decode($encoded_sig); 
     $data = json_decode(base64_url_decode($payload), true); 

     if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { 
      error_log('Unknown algorithm. Expected HMAC-SHA256'); 
      return null; 
     } 

     // check sig 
     $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
     if ($sig !== $expected_sig) { 
      error_log('Bad Signed JSON signature!'); 
      return null; 
     } 

     return $data; 
    } 

    function base64_url_decode($input) { 
     return base64_decode(strtr($input, '-_', '+/')); 
    } 
// 
$data = parse_signed_request($_REQUEST["signed_request"],$application_secret); 

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

    $_SESSION['on'] = 1; 
    include ('src/facebook.php'); 

    $files = array(); 
    $files['RE'] = 'xxx.php'; 
    $files['RC'] = 'yyy.php'; 
    $files['DP'] = 'zzz.php'; 

    $facebook = new Facebook(array(
     'appId' => $app_id, 
     'secret' => $application_secret, 
     'cookie' => true, 
     'perms' => 'publish_stream, offline_access', 
     )); 
    $_SESSION["access_token"] = $data["oauth_token"]; 
    $me = $facebook->api('/me?oauth_token='.$_SESSION["access_token"]); 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" xmlns:fb="https://www.facebook.com/2008/fbml"> 
    <link rel="stylesheet" href="css/style.css" type="text/css"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <meta http-equiv="Content-Style-Type" content="text/css"> 
    <meta property="og:title" content="title" /> 
    <meta property="og:description" content="description" /> 
    <meta property="og:image" content="thumbnail_image" /> 
    </head> 
    <body> 
     <div class="wrapper"> 
      <?php include("inc/app_header.php");?> 
      <div class="content"> 
      <?php 
       if(isset($_GET['section'])) 
       { 
        $file_to_include = 'inc/'.$files[$_GET['section']]; 
       } 
       else 
       { 
        $file_to_include = 'inc/section_restaurantes.php'; 
       } 

       include($file_to_include); 
      ?> 
       <div class="content_bottom_space"></div> 
      </div> 
      <div class="footer"> 
      </div> 
     </div> 
    </body> 
    </html> 
    <?php } ?> 

和section_restaurantes的代碼是:

<div class="section_restaurantes"> 
     <div class="restaurantes_container"> 
      <div class="restaurantes"><a href="index.php?section=DP"></a></div> 
      <div class="restaurantes"><a href="index.php?section=PH"></a></div> 
     </div> 
    </div> 

的事情是,當我在我所有的瀏覽器被重新加載這些div點擊,該?代碼= url參數改變兩次,它再次section_restaurantes.php重新加載,而不是加載DP部分,我希望我是清楚的。

我想是因爲它的重裝兩次我鬆開$ _GET [「節」]參數,然後將其加載默認的是「增量/ section_restaurantes.php」

我需要幫助,請,我試着在互聯網上找到解決方案,但我什麼也沒找到

+0

你知道用PHP-SDK,你並不真的需要解析signed_request你的自我,甚至設置會話,對不對? – ifaour

+0

我不知道如何用另一種方法檢索訪問令牌 –

回答

1

如果您使用的是PHP-SDK,那麼您無需解析signed_request,因爲它可以爲您提供幫助。你只需要檢索用戶(getUser()),如果沒有用戶,重定向到登錄URL(指example文件)。

這裏是一個更好的代碼:

<?php 
include ('src/facebook.php'); 
$app_id = "xxx"; 
$application_secret = 'xxx'; 
$canvas_page = "xxx"; 

$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $application_secret 
)); 

$user = $facebook->getUser(); 
if(!$user) { 
    $loginUrl = $facebook->getLoginUrl(array(
     'redirect_uri' => $canvas_page, 
     'scope' => 'publish_stream,offline_access' 
    )); 
    echo("<script> top.location.href='" . $loginUrl . "'</script>"); 
    exit; 
} 

$_SESSION['on'] = 1; 
$files = array(); 
$files['RE'] = 'xxx.php'; 
$files['RC'] = 'yyy.php'; 
$files['DP'] = 'zzz.php'; 
try { 
    $me = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
} 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" xmlns:fb="https://www.facebook.com/2008/fbml"> 
<link rel="stylesheet" href="css/style.css" type="text/css"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta property="og:title" content="title" /> 
<meta property="og:description" content="description" /> 
<meta property="og:image" content="thumbnail_image" /> 
</head> 
<body> 
    <div class="wrapper"> 
     <?php include("inc/app_header.php");?> 
     <div class="content"> 
     <?php 
      if(isset($_GET['section'])) 
      { 
       $file_to_include = 'inc/'.$files[$_GET['section']]; 
      } 
      else 
      { 
       $file_to_include = 'inc/section_restaurantes.php'; 
      } 

      include($file_to_include); 
     ?> 
      <div class="content_bottom_space"></div> 
     </div> 
     <div class="footer"> 
     </div> 
    </div> 
</body> 
</html> 

現在您的餐廳部分,我會鏈接到父文檔:

<div class="section_restaurantes"> 
    <div class="restaurantes_container"> 
     <div class="restaurantes"><a href="<?php echo $canvas_page; ?>?section=DP" target="_top"></a></div> 
     <div class="restaurantes"><a href="<?php echo $canvas_page; ?>?section=PH" target="_top"></a></div> 
    </div> 
</div> 

假設你$canvas_page是預期的目標。

+0

它工作!非常感謝你 –

+0

不客氣!請嘗試[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)答案。 – ifaour

相關問題