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 利用Java连接API接口总是出问题
  • ¥15 请教一个关于镜头标定,棋盘格格子大小的问题(畸变测试)
  • ¥15 el-table输入多维数组怎么实现
  • ¥15 安装GroudingDINO RuntimeError: Error compiling objects for extension
  • ¥15 关于推送项目到github的问题
  • ¥15 急!C++指针编写相关的问题
  • ¥15 kerberos身份认证配置问题
  • ¥30 用python写一个多签情况下波场的代理资源和回收资源
  • ¥15 怎么在matlab中输出显示泵的流量-扬程和管路损失与流量均在一个表格里
  • ¥15 matlab学期例题代码答疑