2014-01-09 79 views
0

我想創建一個Facebook的畫布應用程序。我正在使用facebook php-sdk和cakephp。Facebook的畫布應用程序oauth對話框頁面錯誤

這是我的登錄功能: -

public function login() { 

     $app_id = "xxxxxxxxxxx"; 
     $app_secret = "xxxxxxxxxxxxxxxxxx"; 
     $canvas_page = "https://apps.facebook.com/xxxxxxx"; 
     $scope = 'email,publish_actions'; 
     $facebook = new Facebook(array(
           'appId' => $app_id, 
           'secret' => $app_secret 
           )); 

     $user = $facebook->getUser(); 

     if ($user) { 

       try { 

       // Proceed knowing you have a logged in user who's authenticated. 
       $user_profile = $facebook->api('/me'); 
       $access_token = $facebook->getAccessToken(); 
       $fbid = $user_profile['id']; 
       pr($user_profile); 

       } catch (FacebookApiException $e) { 

       error_log($e); 
       $user = null; 

       } 

     } else { 

       $loginUrl = $facebook->getLoginUrl(array(
                'scope' => $scope, 
                'redirect_uri' => $canvas_page 
                )); 
       print('<script> top.location.href=\'' . $loginUrl . '\'</script>'); 
     } 

} 

這似乎如果用戶已經通過身份驗證工作。但對於新用戶,而不是顯示OAuth的對話框,它拋出這個錯誤: -

"Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains." 

我在本地主機上測試它。所以我的畫布網址是http://localhost/xxxxx/ 有人可以幫我在這裏嗎?

+0

Facebook的不能檢測到你的本地主機,它必須是一個域名。 –

+0

@AbhikChakraborty是否意味着我無法在本地主機上開發Facebook應用程序?我也嘗試使用虛擬主機,但這也行不通。我還在aws上託管了該站點,並用替換了「localhost」..仍然沒有運氣 – Divyanshu

+0

這是正確的,通常您可能需要一個開發服務器才能啓動該項目。例如我有一個API,我得到一個redirect_uri作爲http:// localhost/xxx的請求,我的代碼不能做重定向,因爲重定向發生在我的本地主機上,而不是你的,但是如果我知道域名I可以重定向到一個域。 –

回答

0

我終於找到了解決辦法: -

兩個步驟: -

一)更改REDIRECT_URI到http://localhost/xxxxxx

B)增加了一個檢查,看是否得到PARAMS有代碼,並設置頭帆布頁。

正確的代碼: -

公共職能的login(){

$app_id = "xxxxxxxxxxx"; 
    $app_secret = "xxxxxxxxxxxxxxxxxx"; 
    $canvas_page = "https://apps.facebook.com/xxxxxxx"; 
    $scope = 'email,publish_actions'; 
    $facebook = new Facebook(array(
          'appId' => $app_id, 
          'secret' => $app_secret 
          )); 
    if (isset($_GET['code'])) { 
     header("Location: " . $canvas_page); 
     exit; 
    } 

    $user = $facebook->getUser(); 

    if ($user) { 

      try { 

      // Proceed knowing you have a logged in user who's authenticated. 
      $user_profile = $facebook->api('/me'); 
      $access_token = $facebook->getAccessToken(); 
      $fbid = $user_profile['id']; 
      pr($user_profile); 

      } catch (FacebookApiException $e) { 

      error_log($e); 
      $user = null; 

      } 

    } else { 

      $loginUrl = $facebook->getLoginUrl(array(
               'scope' => $scope, 
               )); 
      print('<script> top.location.href=\'' . $loginUrl . '\'</script>'); 
    } 

}

相關問題