用Facebook发布PHP api => UOAuthException:(#282)需要扩展权限:抛出share_item

时间:2012-05-08 22:09:08

标签: php facebook facebook-graph-api facebook-opengraph

这是我的代码:

<?
session_start();

require_once($_SERVER['DOCUMENT_ROOT'].'/admin/config.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/fonctions.php');

include '../api/facebook.php';
include '../api/xhttp.php';


  $config = array();
  $config['appId'] = '35455XXXXXXX207';
  $config['secret'] = '6006855f3aXXXXXXXXX9bce45a426';
  $config['fileUpload'] = false; // optional

  $facebook = new Facebook($config);


$args = array(
    'message'   => 'Hello from my App!',
    'link'      => 'http://www.masteringapi.com/',
    'caption'   => 'Visit MasteringAPI.com For Facebook API Tutorials!',
    'access_token'  => $access_token
);

$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];

$post_id = $facebook->api("/me/feed", "post", $args);

?>

但它返回:未捕获OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。 有任何想法吗 ?

谢谢!


这是我的新代码:

<?


  $config = array();
  $config['appId'] = '3545XXXXXXX6207';
  $config['secret'] = '60068XXXXXXXXXXXe45a426';


  $facebook = new Facebook($config);


$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];


$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

if(!isset($_GET['session'])) {
    $params = array(
    'locale' => 'fr_FR',
    'display' => 'popup', 
    'req_perms' => 'email,user_about_me,user_birthday,user_location',
    'ext_perms' => 'publish_stream,share_item,manage_pages'
    );
    header("Location: ".$facebook->getLoginUrl($params));
}



        $args = array(
                'message'   => "XXXX",
                'link'      => 'XXXX',
                'name'      => 'XXXX',
                'picture'   => 'XXXX',
                "caption"=> "lien.com",
                      "type"=> "link");

        $post_id = $facebook->api("/me/links", "post", $args);

?>

这次我收到此错误:OAuthException:(#282)需要扩展权限:抛出share_item

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

您没有将访问令牌传递给它。

查看getAccessToken()。

答案 1 :(得分:0)

在您定义$ access_token变量之前,您的代码示例看起来好像是在尝试使用变量$ access_token来创建$ args数组。我会切换变量定义的顺序:

$retour_login = json_decode(stripslashes($_GET['session']), true);
$uid = $retour_login['uid'];
$access_token = $retour_login['access_token'];

$args = array(
'message'   => 'Hello from my App!',
'link'      => 'http://www.masteringapi.com/',
'caption'   => 'Visit MasteringAPI.com For Facebook API Tutorials!',
'access_token'  => $access_token
);

这是假设$ retour_login ['access_token']包含有效的Facebook访问令牌。否则,您将需要使用Facebook SDK获取当前用户的访问令牌:

$access_token = $facebook->getAccessToken();