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


Uncaught OAuthException: Error validating application

我正在开发我的第一个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);

我在web中找到的最好的工作解决方案。在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'];
   }

即使用户已登录,用户也需要对您的应用程序授予权限,然后才能进行api调用。

基本上,使用facebookphp-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>";
}

希望这能有所帮助。

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