2012-03-08 80 views
0

我正在開發我的第一個Facebook應用程序,其中包括創建新相冊並將照片發佈到用戶牆。通過學習通過Facebook文檔和幾個教程,並提出了這個代碼,但我得到以下錯誤。未捕獲的OAuthException:驗證應用程序時出錯

Fatal error: Uncaught OAuthException: Error validating application. Thrown in /home/base_facebook.php on line 1106

加:我不知道,如果它的事項或不是,但是當我的回報,即使用戶打印出

echo $facebook->getUser();

變量,它給我的「0」 (在這種情況下是我自己)已登錄。

請幫助我解決這個問題。

代碼:

$config = array(
'appId' => '3663250024856', 
'secret' => 'b14ac6d2b7dbdtyb259599b06983e881', 
'fileUpload' => true, 
'cookie' => true 
); 


     $facebook = new Facebook($config); 
     echo "USER STATUS:".$facebook->getUser(); 
     $facebook -> setFileUploadSupport(true); 

     $album_details = array('message' => 'Album desc', 'name' => 'Album name'); 
     $create_album = $facebook -> api('/me/albums', 'post', $album_details); 

     $album_uid = $create_album['id']; 

     $photo_details = array('message' => 'Photo message'); 
     $file = "temp/".$imageName; 
     $photo_details['image'] = '@' . realpath($file); 

     $upload_photo = $facebook -> api('/' . $album_uid . '/photos', 'post', $photo_details); 

回答

3

即使用戶登錄,用戶需要賦予權限,以您的應用程序,然後才能進行API調用。

基本上使用Facebook PHP SDK用戶認證的流程應該是這樣的:

  1. 檢測用戶是否登錄並獲准您的應用程序。

  2. 如果他沒有給予權限,將他重定向到登錄和應用程序權限頁面。登錄並授予權限後,他將被重新導回到您的應用程序頁面。

    否則,顯示您的應用程序的內容,進行API調用,...

這裏是從PHP SDK文檔的頁面來處理身份驗證修改的示例代碼,如果你只是修改應工作appId,appSecret和redirect_uri:

<?php 
require_once 'fb_php_sdk/facebook.php'; 

$appId = 'your_app_id'; 
$appSecret = 'your_app_secret'; 

// Create your Application instance (replace this with your appId and secret). 
$facebook = new Facebook( 
    array(
     'appId' => $appId, 
     'secret' => $appSecret, 
    'fileUpload' => true, 
    'cookie' => true 
)); 

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

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(
           'canvas' => 1, 
           'fbconnect' => 0, 
           'scope' => '',//these are the extended permissions you will need for your app, add/remove according to your requirement 
           'redirect_uri' => 'http://apps.facebook.com/test_app_azk/',//this is the redirect uri where user will be redirected after allow, 
           ));          
} 

if ($user) 
{ 
    //show the content of your app 
    //make api calls 
    var_dump($user_profile); 
} 
else 
{ 
    //redirect user to loginUrl 
    echo "<script>parent.location = '".$loginUrl."';</script>"; 
} 

希望這會有所幫助。

+0

如果我沒看錯的權限時給出用戶首次授權的應用..用戶不需要每次他使用的應用程序或時間賦予權限他呢?在第一種情況下,用戶已經授權該應用程序?請清除它。 – Maven 2012-03-08 06:41:07

+0

是的,你是對的。用戶只有在他第一次訪問您的應用時才需要授予權限。之後,將用戶重定向到loginUrl會將其帶回帶有有效access_token的應用程序頁面,從而使應用程序能夠進行api調用。 – 2012-03-08 06:48:01

+0

你能PLZ指導我如何檢索'access_token'後進行身份驗證,並重定向到應用程序頁面? – Maven 2012-03-08 12:56:57

1

問題可能在於else語句中。而不是使用其他的使用if(!user)。這應該解決這個問題。

4

我在網上找到的最佳工作解決方案。添加下面的代碼在你的PHP頁面的頂部:

$app_id = "YOUR_APP_ID"; 
$app_secret = "YOUR_APP_SECRET"; 
$my_url = "YOUR_URL"; 


    $code = $_REQUEST["code"]; 

    if(empty($code)) { 
    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection 
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
     . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" 
     . $_SESSION['state']; 

    echo("<script> top.location.href='" . $dialog_url . "'</script>"); 
    } 

    if($_REQUEST['state'] == $_SESSION['state']) { 
    $token_url = "https://graph.facebook.com/oauth/access_token?" 
     . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) 
     . "&client_secret=" . $app_secret . "&code=" . $code; 

    $response = @file_get_contents($token_url); 
    $params = null; 
    parse_str($response, $params); 

    $graph_url = "https://graph.facebook.com/me?access_token=" 
     . $params['access_token']; 
    } 
相關問題