如何使用已保存在我的数据库中的facebook访问令牌在Facebook上发布

时间:2018-01-13 18:04:56

标签: php facebook facebook-graph-api sdk

我使用facebook api在我的网站上收集用户facebook长期访问令牌并将其保存在数据库中。现在我想从数据库中检索它们并在墙上发布。但是我收到以下错误消息:

  

Facebook SDK返回错误:您必须提供访问令牌。

他们已经给了我必要的权限,我已经从数据库

提供了访问令牌

代码:

<?php



 define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
    require_once(__DIR__.'/src/Facebook/autoload.php');
 $fb = new Facebook\Facebook([
'app_id' => '***************',

'app_secret' => '***************',

'default_graph_version' => 'v2.11',

]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email','publish_actions','manage_pages','publish_pages']; 
// optional
$query = 'SELECT * FROM music_promotion';
$stmt = mysqli_query ($dbc,$query);

while ($row=mysqli_fetch_array($stmt)){
$_SESSION['facebook_access_token'] = $row['fb_access_token'];

try {

    if (isset($_SESSION['facebook_access_token'])) {

        $accessToken = $_SESSION['facebook_access_token'];

    } else {

        $accessToken = $helper->getAccessToken();

    }

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

}

if (isset($accessToken)) {

    if (isset($_SESSION['facebook_access_token'])) {

        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

    } else {

        // getting short-lived access token

        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler

        $oAuth2Client = $fb->getOAuth2Client();

        // Exchanges a short-lived access token for a long-lived one

        $longLivedAccessToken = $oAuth2Client-
>getLongLivedAccessToken($_SESSION['facebook_access_token']);

        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

        // setting default access token to be used in script

        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

    }


    try {

        //Post property to Facebook


        $linkData = [
            'link' => 'the link',
            'message' => 'the message'
        ];
        $pageAccessToken = $_SESSION['facebook_access_token'];

        try {
            $response = $fb->post('/me/feed', $linkData, $pageAccessToken);
        } 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;
        }
        $graphNode = $response->getGraphNode();

    } catch(Facebook\Exceptions\FacebookResponseException $e) {

        // When Graph returns an error

        echo 'Graph returned an error: ' . $e->getMessage();

        session_destroy();

        // redirecting user back to app login page

        header("Location: ./");

        exit;

    } catch(Facebook\Exceptions\FacebookSDKException $e) {

        // When validation fails or other local issues

        echo 'Facebook SDK returned an error: ' . $e->getMessage();

        exit;

    }

  }
}

0 个答案:

没有答案