2011-10-02 3 views
1

OAuth 2.0으로 업그레이드 할 때까지 내 facebook 응용 프로그램에서 evertything이 잘 실행되고 있었고 메신저가 모든 것을 올바르게 수행하고 있는지 확실하지 않았습니다.내 Facebook 응용 프로그램에서 OAuth 2.0에 문제가 발생했습니다.

것은 이미 사용자가 응용 프로그램을 승인 할 때,가 Iframe에 내 응용 프로그램을 렌더링,하지만 난 [], 내가 설명 할 수 내 $의 _GETs에 문제가 발생하고 있고 일할 수있는 OAuth를 대화 만들어진 것입니다 :

$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 } ?> 

과를 : 난 그냥() 사업부의 일부 파일이 $ _GET에 따라 콘텐츠를 불리는 [ '섹션'] 포함하면서

여기, 내가 메인 페이지로 사용 제의 index.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의? code = 매개 변수가 두 번 변경되고 DP 섹션을로드하는 대신 section_restaurantes.php에 다시로드됩니다. .

나는 그 두 번 다시로드하기 때문에 내가 $ _GET [ '섹션'] 매개 변수를 느슨하게 생각하고 다음은 "INC/section_restaurantes.php"나는 도움, I가 시도한하시기 바랍니다 필요

은 기본값을로드 인터넷에서 해결책을 찾았지만 아무 것도 발견하지 못했습니다.

+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

관련 문제