0
votes

i'm using PHP 4 SDK for Facebook Graph API to get insights data from a specified facebook page.

Therefore we have a test user who has admin rights for this page. Fetching public profile data is no problem but when i try to fetch insights data e.g. with this request

/{post-id}?fields=shares,likes.summary(true),comments.summary(true)

i don't get the shares value because requesting the required permissions ('manage_pages, read_insights, read_stream') fails.

I detected this fact when i debugged the access token and inserted it in Facebook Graph API Explorer, saying only 'public_profile' permission.

Although i can insert the same request directly into my graph explorer and getting the correct response, i don't get it working with my PHP script.

Right now my script looks like this:


    ob_start();
    session_start();

    // added in v4.0.0
    require_once 'autoload.php';

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\Entities\AccessToken;
    use Facebook\HttpClients\FacebookCurlHttpClient;
    use Facebook\HttpClients\FacebookHttpable;

    // init app with app id and secret
    FacebookSession::setDefaultApplication('app-id', 'app-secret');

    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('http://facebook.local/');

    try {
      $session = $helper->getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
    } catch( Exception $ex ) {
      // When validation fails or other local issues
    }

    // see if we have a session
    if ( isset( $session ) ) {

      print_r($session->getToken());

      // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();
      // get response
      $graphObject = $response->getGraphObject();

      // print data
      echo '
' . print_r( $graphObject, 1 ) . '
'; } else { $params = array( 'scope' => 'manage_pages, read_insights, read_stream' ); // show login url echo 'getLoginUrl($params) . '">Login'; }

Has anybody any ideas what i am doing wrong?

I would very appreciate your help.

Thanks in advance. Axel

1

1 Answers

0
votes

Change your code to:

  $scope = array( 'manage_pages', 'read_insights', 'read_stream' );

  // show login url
  echo '<a href="'. $helper->getLoginUrl( $scope ) . '">Login</a>';

The scope is a array with permissions. The login URL is accessed from the $helper.