drw85135 2015-07-26 19:27
浏览 55
已采纳

Facebook使用php sdk5 graph api 2.4发布到粉丝页面

I'm trying to create a automatic post script to fanpage using php SDK v5 and Graph API 2.4 !

I created two scripts:

1 - To get the list of page access tokens using publish_actions and manage_pages permissions. (returning ok)

2 - Offline publish to fanpage. (fail)

1) The first script works fine:

<?php
session_start(); 

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

// config APP_ID e SECRET
define('APP_ID', 'xxxxxxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');

// instancia
$fb = new Facebook\Facebook([
    'app_id'     => APP_ID,
    'app_secret' => APP_SECRET,
    'default_graph_version' => 'v2.4'
]);

if(isset($_SESSION['fb_access_token'])) {
    $accessToken = $_SESSION['fb_access_token'];

} else {

    $helper = $fb->getRedirectLoginHelper();
    try {
        $accessToken = $helper->getAccessToken();

        if(isset($accessToken)) {
            $oAuth2Client = $fb->getOAuth2Client();

            // longlived access token
            if (!$accessToken->isLongLived()) {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); 
            }
        }

    } 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;
    }
}

if(isset($accessToken)) {
    // Logged in!
    $_SESSION['fb_access_token'] = (string) $accessToken;

    try {
        $response = $fb->get('/me/accounts', $accessToken);

        echo '<pre>';
        print_r($response->getDecodedBody());
        echo '</pre>';

    } 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;
    } 
} else {
    $helper = $fb->getRedirectLoginHelper();

    $redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $loginUrl = $helper->getLoginUrl($redirect_url, [ 'manage_pages', 'publish_actions' ]);
    echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}

2) The second script fails: (the script should work even if I am disconnected from facebook):

<?php
session_start(); 

// autoloader 
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

define('APP_ID', 'xxxxxxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');
$page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// instance
$fb = new Facebook\Facebook([
    'app_id'     => APP_ID,
    'app_secret' => APP_SECRET,
    'default_graph_version' => 'v2.4',
]);

$linkData = [
  'link' => 'http://www.example.com',
  'message' => 'User provided message',
];

try {
    $response = $fb->post('/me/feed', $linkData, $page_access_token);

} 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;
}

I`m getting the error:

Graph returned an error: Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.

In old graph api versions with php sdk v3, it works in a similar way...

What's wrong ?

  • 写回答

1条回答 默认 最新

  • doufu6504 2015-07-26 20:24
    关注

    As i suggested in the comments, you need publish_pages to post as Page. After authorizing with publish_pages, you can get a Page Token with /me/accounts and use the resulting Token to post as Page.

    Make sure you are using an Extended Page Token if you want to store it. It is valid forever.

    More information about Access Tokens:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化