简体   繁体   English

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

[英]Facebook API. Can't publish on page with valid access token from PHP

I'm stuck trying to publish in a facebook page using the SDK in PHP 5.5. 我被困在尝试使用PHP 5.5中的SDK在Facebook页面中发布。 I want to make a php script to auto publish updates in a facebook page that I administer. 我想制作一个php脚本,以在我管理的Facebook页面中自动发布更新。 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. 当我使用Graph API Explorer发出请求时,它可以完美运行,但是从PHP脚本执行操作时,总是收到错误消息“(#200)用户没有授权应用程序执行此操作”。

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. 使用Graph API Explorer的“获取访问令牌”选项,为我的应用程序获取短期访问令牌,其权限为manage_pages和publish_pages。
  2. Get a never expire access token using the getOAuth2Client of the PHP SDK. 使用PHP SDK的getOAuth2Client获取永不过期的访问令牌。
  3. Get the page access token with the "/me/accounts" request 通过“ / me / accounts”请求获取页面访问令牌
  4. Send the publish request using the page access token and method "PORT /pageId/feed" with params: access_token and message 使用页面访问令牌和方法“ PORT / pageId / feed”使用以下参数发送发布请求:access_token和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. 获取页面访问令牌后,如果我使用Graph API Explorer进行发布请求,则它可以工作,但是从PHP脚本中,它将失败。

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. 所有令牌均有效,请使用Facebook Access令牌调试器进行检查。 The page token info contains the app id, page id and my user id, with the manage_pages, publish_pages and public_profile permissions. 页面令牌信息包含应用程序ID,页面ID和我的用户ID,以及manage_pages,publish_pages和public_profile权限。

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). 该应用程序是“实时的”,未提交审批(我只是创建了该应用程序以在Facebook页面中自动发布消息)。

I've read a lot of messages and examples here and in Internet but I do not get it to work. 我在这里和Internet上已经阅读了许多消息和示例,但是我无法使其正常工作。 I hope anyone can help me. 我希望有人能帮助我。

Thank you very much! 非常感谢你!

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. 我意识到我没有使用页面的访问令牌,而是在使用/me/访问令牌。

I have permissions for publish_pages and manage_pages . 我具有publish_pagesmanage_pages权限。

For reference, I'm using the Lumen framework. 作为参考,我正在使用Lumen框架。

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']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM