首页 > 解决方案 > 无法在用户时间轴上发布

问题描述

如何使用 php sdk 在用户时间轴上发布

我在stackoverflow上搜索这个问题我没有得到任何plx帮助我需要为我的客户做这件事

   <?php
   namespace App;

   use Facebook\Facebook;

   class FacebookClass{
   private $fb;
   private $appiD = 3073019182770120;
   private $appSecret = "6360e1540e1add2b6fc93fce07fcb61a";

   public function __construct()
   {
    $this->fb = new Facebook([
        'app_id' => $this->appiD, // Replace {app-id} with your app id
        'app_secret' => $this->appSecret,
        'default_graph_version' => 'v4.0',
    ]);
   }

   public function getLoginUrl() {
    $helper = $this->fb->getRedirectLoginHelper();
    $permissions = ['email']; // Optional permissions
    $loginUrl = $helper- 
   >getLoginUrl('http://localhost/social_platform/',$permissions);

    return $loginUrl;
    }

   public function loginWithFacebook() {
   $helper = $this->fb->getRedirectLoginHelper();
   try {
   $accessToken = $helper->getAccessToken();
   if(isset($accessToken))
   {
    $_SESSION['facebook']['access_token'] = (string) $accessToken;

    header("index.php");
   }
   } catch (\Throwable $th) {
// var_dump($th);
   }
   }

   public function postOnUserTimeLine() {


    // posting on user timeline using publish_actins permission
    try {
        // message must come from the user-end
        $data = ['message' => 'testing...'];
        $request = $this->fb->post('/me/feed', $data, 
         $_SESSION['facebook']['access_token']);
        $response = $request->getGraphNode()->asArray;
    } catch (Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    echo $response['id'];

    }




   }



   ?>

致命错误:未捕获的 Facebook\Exceptions\FacebookAuthorizationException: (#200) 如果发布到群组,则需要在群组中安装应用程序,并且 \ 具有用户令牌的 publish_to_groups 权限,或者具有页面令牌的 manage_pages \ 和 publish_pages 权限;如果发布到页面,\ 要求 manage_pages 和 publish_pages 作为管理员在 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\Exceptions\FacebookResponseException.php:137 中有足够的管理权限跟踪:#0 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(210): Facebook\Exceptions\FacebookResponseException::create(Object(Facebook\FacebookResponse)) #1 E:\xampp\htdocs\Social_Platform\vendor\facebook\graph-sdk\src\Facebook\FacebookResponse.php(255): Facebook\FacebookResponse->

标签: phpfacebook-graph-apifacebook-php-sdk

解决方案


不能再发布到用户墙了。所需的权限“publish_actions”不久前被弃用:

https://developers.facebook.com/docs/graph-api/changelog/break-changes#login-4-24

改用共享对话框:https ://developers.facebook.com/docs/sharing/reference/share-dialog


推荐阅读