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

时间:2017-09-18 16:36:06

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

使用将网页与群组(https://grytics.com/blog/link-facebook-groups-pages/)连接起来的新功能,我可以通过图谱API在群组Feed中将其作为网页发布。我的所有尝试都以错误说:

  

页面上没有边缘/节点“组”

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中找到了用户评论:

  

不,没有内置方式可以让网页帖子显示在群组中   自动。

0 个答案:

没有答案