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

问题描述:

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

即使用户登录,用户需要赋予权限,以您的应用程序,然后才能进行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

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

我在网上找到的最佳工作解决方案。添加下面的代码在你的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']; 
    }