尝试使用php发布到Facebook时出现FacebookPermissionException错误"用户尚未授权应用程序执行此操作"

时间:2015-03-27 09:01:02

标签: php facebook facebook-graph-api permissions

我在尝试使用php发布到Facebook墙时收到错误。 错误如下~PHP致命错误:未捕获异常' Facebook \ FacebookPermissionException'消息'(#200)用户未授权应用程序执行此操作'在/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequestException.php:128

我正在使用的代码是

// include required files form Facebook SDK
$dir = 'facebook-php-sdk-v4-4.0-dev/src/';

require_once( $dir.'Facebook/HttpClients/FacebookHttpable.php' );
require_once( $dir.'Facebook/HttpClients/FacebookCurl.php' );
require_once( $dir.'Facebook/HttpClients/FacebookCurlHttpClient.php' );

require_once( $dir.'Facebook/Entities/AccessToken.php' );
require_once( $dir.'Facebook/Entities/SignedRequest.php' );

require_once( $dir.'Facebook/FacebookSession.php' );
require_once( $dir.'Facebook/FacebookRedirectLoginHelper.php' );
require_once( $dir.'Facebook/FacebookRequest.php' );
require_once( $dir.'Facebook/FacebookResponse.php' );
require_once( $dir.'Facebook/FacebookSDKException.php' );
require_once( $dir.'Facebook/FacebookRequestException.php' );
require_once( $dir.'Facebook/FacebookOtherException.php' );
require_once( $dir.'Facebook/FacebookAuthorizationException.php' );
require_once( $dir.'Facebook/GraphObject.php' );
require_once( $dir.'Facebook/GraphSessionInfo.php' );

require_once( $dir.'Facebook/FacebookClientException.php' );
require_once( $dir.'Facebook/FacebookPermissionException.php' );
require_once( $dir.'Facebook/FacebookServerException.php' );
require_once( $dir.'Facebook/FacebookThrottleException.php' );

use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;

use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;

use Facebook\FacebookClientException;
use Facebook\FacebookPermissionException;
use Facebook\FacebookServerException;
use Facebook\FacebookThrottleException;

// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( 'app-id' , 'secret' );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://f13.tv/share/test01.php' );



// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {

    // create new session from saved access_token
    $session = new FacebookSession( $_SESSION['fb_token'] );

    $page_id = '187295794639218';

    // get page access token
    $access_token = (new FacebookRequest( $session, 'GET', '/' . $page_id,  array( 'fields' => 'access_token' ) ))
    ->execute()->getGraphObject()->asArray();

    // save access token in variable for later use  
    $access_token = $access_token['access_token'];

    // post to page
    $page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id.'/feed', array(
        'access_token' => $access_token,
        'name' => 'Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x',
        'link' => 'https://www.webniraj.com/2014/08/23/facebook-api-posting-as-a-page-using-graph-api-v2-x-and-php-sdk-4-0-x/',
        'caption' => 'The Facebook API lets you post to Pages you administrate via the API. This tutorial shows you how to achieve this using the Facebook PHP SDK v4.0.x and Graph API 2.x.',
        'message' => 'Check out my new blog post!',   ) ))->execute()->getGraphObject()->asArray();

    // return post_id
    print_r( $page_post );

}

2 个答案:

答案 0 :(得分:2)

发生此错误是因为您无权在网页上发帖。左页需要发布权限 publish_actions
您的登录URL如下所示:
$loginUrl = $helper->getLoginUrl(array('publish_actions')); / *登录网址有权限* /
为了检查用户执行的授权这样的事情:
$user_permissions = (new FacebookRequest($session, 'GET', '/me/permissions'))->execute()->getGraphObject(GraphUser::className())->asArray();
var_dump($user_permissions);

答案 1 :(得分:0)

这是自上周以来影响数千名开发人员的已知错误,请关注此错误报告并发布以加快Facebook: https://developers.facebook.com/bugs/1655744048061679/