使用Facebook Graph v5 sdk在Facebook上发布时出错

时间:2018-07-20 06:26:52

标签: facebook-graph-api

我正在使用Facebook Graph v5 sdk进行登录和张贴在墙上,但是出现一些错误。我已经成功登录并成功获取了用户的年龄,生日等详细信息,但是在使用此代码在Facebook上发布时

$fb->post('/me/feed', $attachment, $accessToken);

出现一些权限错误,如下所示。

错误:     致命错误:未捕获的Facebook \ Exceptions \ FacebookAuthorizationException:(#200)需要publish_actions权限,或以具有足够管理权限的管理员身份需要manage_pages和publish_pages

index.php

<?php

    require_once "config.php";
    $redirectURL = "http://localhost/fbLogin/fb-callback.php";
    $permissions = ['email, user_birthday,user_posts,manage_pages,publish_pages'];
    $loginURL = $helper->getLoginUrl($redirectURL, $permissions);
    echo $loginURL;
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <button onclick="myFunction()">Click me</button>


<script>
function myFunction() {
  window.location = "<?php echo $loginURL; ?>";
}
</script>
</body>
</html>

fb-callback.php

<?php
    require_once "config.php";

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

    } catch(\fbLogin\Exceptions\FacebookResponseException $e) {
        echo $e->getMessage();
        exit();

    } catch(\fbLogin\Exceptions\FacebookResponseException $d) {
        echo $d->getMessage();
        exit();

    }
    if (!isset($accessToken)) {
      if ($helper->getError()) {
        header('HTTP/1.0 401 Unauthorized');
        echo "Error: " . $helper->getError() . "\n";
        echo "Error Code: " . $helper->getErrorCode() . "\n";
        echo "Error Reason: " . $helper->getErrorReason() . "\n";
        echo "Error Description: " . $helper->getErrorDescription() . "\n";
      } else {
        header('HTTP/1.0 400 Bad Request');
        echo 'Bad request';
      }
      exit;
    }

    // Logged in
    echo '<h3>Access Token</h3>';
    var_dump($accessToken->getValue());

    // The OAuth 2.0 client handler helps us manage access tokens
    $oAuth2Client = $fb->getOAuth2Client();

    // Get the access token metadata from /debug_token
    $tokenMetadata = $oAuth2Client->debugToken($accessToken);
    echo '<h3>Metadata</h3>';
    var_dump($tokenMetadata);

    // Validation (these will throw FacebookSDKException's when they fail)
    $tokenMetadata->validateAppId('371331840061100'); // Replace {app-id} with your app id
    // If you know the user ID this access token belongs to, you can validate it here
    //$tokenMetadata->validateUserId('123');
    $tokenMetadata->validateExpiration();

    if (! $accessToken->isLongLived()) {
      // Exchanges a short-lived access token for a long-lived one
      try {
        $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
      } catch (Facebook\Exceptions\FacebookSDKException $e) {
        echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
        exit;
      }

      echo '<h3>Long-lived</h3>';
      var_dump($accessToken->getValue());
    }

    $_SESSION['fb_access_token'] = (string) $accessToken;
    $respose = $fb->get("/me?fields=id,name,email,first_name,last_name,address,hometown,gender,birthday,posts", $accessToken);
    // Get the base class GraphNode from the response
    $graphNode = $respose->getGraphNode();
    // Get the response typed as a GraphUser
    $user = $respose->getGraphUser();
    echo $user->getName();
    echo $user->getEmail();
    echo $user->getBirthday()->format('m/d/y');
    echo $graphNode->getField('country');
    echo $user->getHomeTown();
    echo $user->getGender();

    echo $_SESSION['fb_access_token'];
    header('Location: http://localhost/fblogin/home.php');
?>

config.php

<?php

    session_start();
    require_once "Facebook/autoload.php";
    $fb  = new \Facebook\Facebook([

            'app_id' => '371331840061100',
            'app_secret' =>'fc535825bfe084a63c33a6d36648ddff',
            'default_graph_version' => 'v2.10'

        ]);
    $helper = $fb->getRedirectLoginHelper();

?>

home.php

<?php
    require_once "config.php";
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<h1>wlecom</h1>

<?php



    $message = "test message";

    $attachment = array('message' => $message);

    try{

        $accessToken = $_SESSION['fb_access_token'];

             // Post to Facebook
        $fb->post('/me/feed', $attachment, $accessToken);

        // Display post submission status
        echo 'The post was published successfully to the Facebook timeline.';

    }catch(FacebookResponseException $e){
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    }catch(FacebookSDKException $e){
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;


    } catch(\fbLogin\Exceptions\FacebookResponseException $e) {
        echo $e->getMessage();
        exit();

    } catch(\fbLogin\Exceptions\FacebookResponseException $d) {
        echo $d->getMessage();
        exit();
    }


?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试使用不包含publish_actions权限的令牌在用户墙上发布。

您应该首先阅读有关publish_actions的变更日志:https://developers.facebook.com/docs/graph-api/changelog/breaking-changes