Facebook API-在社区组中作为页面发布

问题描述 投票:2回答:1

具有将页面与组连接起来的新功能(https://grytics.com/blog/link-facebook-groups-pages/),我很坚信,可以通过Graph API将页面发布为组供稿。我所有的尝试都以错误说出:

页面上没有边缘/节点'组'

require_once "./vendor/autoload.php";

use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Authentication\OAuth2Client;
use Facebook\Authentication\AccessToken;
use Facebook\Helpers\FacebookRedirectLoginHelper;

$fb = new Facebook([
    'app_id' => '123456',
    'app_secret' => 'abcde',
    'default_graph_version' => 'v2.10',
    //'default_access_token' => PAGE_TOKEN, // optional
]);

$helper = $fb->getCanvasHelper();
$permissions = ['user_managed_groups', 'publish_actions', 'manage_pages', 'publish_pages'];

$tokenFileName = "./fb-token.txt";
$date = new DateTime();
$cityToPost = 'Berlin';
$autopostGroups = array();
$message = array(
    'message' => 'TEST Post: '.$date->getTimestamp(),
    'link' => 'https://domain.de'
);

try {
    // Refresh longlicedaccesstoken with new one
    if (file_exists($tokenFileName)) {
        $token = file_get_contents($tokenFileName);
        $lastTokenRefresh = time() - filemtime($tokenFileName);
        if ($lastTokenRefresh > 60 * 60 * 24 * 7)
        {
            $oAuth2Client  = $fb->getOAuth2Client();
            $newToken      = $oAuth2Client->getAccessTokenFromCode(
                $oAuth2Client->getCodeFromLongLivedAccessToken(
                    $token
                )
            );
            file_put_contents($tokenFileName, (string) $newToken);
            $accessToken                       = (string) $newToken;
        }else{
            $accessToken                       = (string) $token;
        }
    } else {
        $accessToken = $helper->getAccessToken();
    }
} catch(FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    unlink($tokenFileName);
    exit;
} catch(FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    unlink($tokenFileName);
    exit;
}

if (isset($accessToken)) {
    if (file_exists($tokenFileName)) {
        $fb->setDefaultAccessToken(file_get_contents($tokenFileName));
    } else {
        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken((string) $accessToken);
        file_put_contents($tokenFileName, (string) $longLivedAccessToken);
        $fb->setDefaultAccessToken($longLivedAccessToken);
    }
    // redirect user back to app when page receives $_GET['code'] variable
    if (isset($_GET['code'])) {
        echo "<script>window.top.location.href='https://apps.facebook.com/xyz/';</script>";
        exit;
    }

    // validating the access token
    try {
        $request = $fb->get('/me');
    } catch(FacebookResponseException $e) {
        // When Graph returns an error
        if ($e->getCode() == 190) {
            unlink($tokenFileName);
            //unset($_SESSION['facebook_access_token']);
            $helper = $fb->getRedirectLoginHelper();
            $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/xyz/', $permissions);
            echo "<script>window.top.location.href='".$loginUrl."'</script>";
            exit;
        }
    } catch(FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // get list of groups managed by user
    try {
        $requestGroups = $fb->get('/me/groups');
        $groups = $requestGroups->getGraphEdge()->asArray();

    } catch(FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    foreach ($groups as $group) {
        if(strpos($group['name'], 'VENDOR') !== false && strpos($group['name'], $cityToPost) !== false){
            array_push($autopostGroups,$group['id']); //Only for debugging

            try {
                $requestPost = $fb->post('/'.$group['id'].'/feed', $message);
                $post = $requestPost->getGraphNode()->asArray();

                var_dump($post);
            } catch(FacebookResponseException $e) {
                // When Graph returns an error
                echo 'Graph returned an error: ' . $e->getMessage();
                exit;
            } catch(FacebookSDKException $e) {
                // When validation fails or other local issues
                echo 'Facebook SDK returned an error: ' . $e->getMessage();
                exit;
            }
        }

    }
    // Now you can redirect to another page and use the access token from $tokenFileName
} else {
    $helper = $fb->getRedirectLoginHelper();
    $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/xyz/', $permissions);
    echo "<script>window.top.location.href='".$loginUrl."'</script>";
}

我是fb api的新手,而且我不是英语母语人士,所以也许有人可以澄清这种情况?

谢谢!

[编辑1]

添加了代码。提示:当我将PAGE_TOKEN更改为USER_TOKEN时,它可以工作。

[编辑2]

已添加完整代码。我从八月开始在FB组Facebook Developer Community中找到了用户评论:

[不,没有内置方法可以将页面帖子显示在群组中自动。

php facebook facebook-graph-api facebook-php-sdk
1个回答
0
投票

我的声誉不高,无法发表评论,因此我将我的问题作为答案。

发布此问题已经2年了,而我也遇到了同样的问题。我使用Facebook登录名登录我的应用并获取用户令牌。然后,我检索页面令牌。我可以成功地使用用户令牌(到/ groupid / feed)发布消息,但是当我尝试使用页面令牌以完全相同的方式发布消息时-graphRequest失败,并显示一条错误消息,提示我尝试访问未访问的对象存在。

我是一个小组和一个页面的管理员,我正在编写一个可帮助我发布到该小组的应用。在大多数情况下,帖子应发布为页面。

您(或其他任何人)是否找到了一种解决方案,允许您使用GraphApi以页面而不是以用户身份发布组状态更新?

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.