duanmianxue2687 2015-09-05 09:56
浏览 76

Facebook API。 无法使用PHP中的有效访问令牌在页面上发布

I'm stuck trying to publish in a facebook page using the SDK in PHP 5.5. I want to make a php script to auto publish updates in a facebook page that I administer. When I make the request using the Graph API Explorer it works perfect, but I always receive the error message "(#200) The user hasn't authorized the application to perform this action" when doing from the PHP script.

These are the steps and code I'm using:

  1. Get a short-lived access token for my application using the "Get Access Token" option of the Graph API Explorer, with permissions manage_pages and publish_pages.
  2. Get a never expire access token using the getOAuth2Client of the PHP SDK.
  3. Get the page access token with the "/me/accounts" request
  4. Send the publish request using the page access token and method "PORT /pageId/feed" with params: access_token and message

After getting the page access token, if I do the publish request using the Graph API Explorer it works, but from the PHP script it fails.

Here is the complete code I'm using:

<?php
require_once __DIR__ . '/Facebook/autoload.php';

$appId = 'my_app_id';
$appSecret = 'my_app_secret';
$pageId = 'my_page_id';

$shortToken = 'token got in step 1';

$fb = new Facebook\Facebook([
  'app_id' => $appId,
  'app_secret' => $appSecret,
  'default_graph_version' => 'v2.2',
]);

$oAuth2Client = $fb->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($shortToken);

echo '<p>Long-live token: ' . $longLivedAccessToken . '</p>';

// Get page access_token
$request = $fb->request('GET', '/me/accounts');
$request->setParams(['access_token' => $longLivedAccessToken]);
try {
  $response = $fb->getClient()->sendRequest($request);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$graphEdge = $response->getGraphEdge();
foreach ($graphEdge as $graphNode) {
  if ($graphNode['id'] == $pageId) {
    $pageAccessToken = $graphNode['access_token'];
  }
}
echo '<p>Page access token: ' . $pageAccessToken . '</p>';

$request = $fb->request('POST', '/' . $pageId . '/feed');
$request->setParams(['access_token' => $pageAccessToken, 'message' => 'Page publish test message']);

try {
  $response = $fb->getClient()->sendRequest($request);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
?>

An output example:

Long-live token: XXXX
Page access token: XXX
Graph returned an error: (#200) The user hasn't authorized the application to perform this action

All the tokens are valid, checked using the Facebook Access Token Debugger. The page token info contains the app id, page id and my user id, with the manage_pages, publish_pages and public_profile permissions.

My application has default settings. I'm just added a platform of type "Website" with the URL of my webpage. The app is "live" and not submitted for approval (I've just create the application to auto publish messages in a Facebook page).

I've read a lot of messages and examples here and in Internet but I do not get it to work. I hope anyone can help me.

Thank you very much!

  • 写回答

1条回答 默认 最新

  • drnysdnnb2909701 2016-03-20 11:47
    关注

    For what it's worth. I was having the same issue. I realised I wasn't using the page's access token, I was using the /me/ access token.

    I have permissions for publish_pages and manage_pages.

    For reference, I'm using the Lumen framework.

    public function submitStock(Request $request){
    
      //first get Page token data from /me/accounts
      $pageTokenData = $this->fb->get("/me/accounts", $_SESSION['facebook_access_token'])->getDecodedBody();
    
      $pageToken = null;
    
      //check get the token for the desired app
      foreach($pageTokenData['data'] as $account){
        if($account['name'] == "AppName"){
          $pageToken = $account['access_token'];
        }
      }
    
      //set message text from POST
      $message = $request->input('message');
    
      //send post using the Pages app token, not the /me/ token
      $this->fb->post('/'.$this->page.'/feed', 
        ['message' => $message], 
        $pageToken);
    
    
      return redirect($_SERVER['HTTP_REFERER']);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图