未捕获OAuthException:验证应用程序时出错

时间:2012-03-08 05:51:16

标签: php facebook facebook-graph-api

我正在开发我的第一个Facebook应用程序,其中包括创建新相册并将照片发布到用户墙。通过学习facebook文档和一些教程,并提出了这个代码,但我得到了跟随它的错误。

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 个答案:

答案 0 :(得分: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'];
   }

答案 1 :(得分:3)

即使用户已登录,用户也需要先向您的应用授予权限,然后才能进行API呼叫。

基本上使用facebook php sdk进行用户身份验证的流程应该是这样的:

  1. 检测用户是否已登录并获得您应用的权限。

  2. 如果他未授权,请将其重定向到登录和申请许可页面。登录并授予权限后,他将被重定向回您的应用程序页面。

    否则,请显示您的应用内容,进行api通话,...

  3. 以下是来自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>";
    }
    

    希望这有帮助。

答案 2 :(得分:1)

问题可能在于else语句中。而不是使用else if(!user)。这应该可以解决问题。

相关问题