Search code examples
phpfacebookfacebook-graph-apitokenaccess-token

accessing facebook app with app token


I have a little issue getting access to some ad data via an app

I have these two scripts:

Login

<?php


    include_once __DIR__ . '/includes/connect.php';
    require_once __DIR__ . '/vendor/autoload.php';

    use Facebook\Facebook;

    session_start();

    $facebook = new Facebook([
        'app_id' => 'XXX',
        'app_secret' => 'XXX',
        'default_graph_version' => 'v2.5',
        ]);

    $helper = $facebook->getRedirectLoginHelper();

    $permissions = ['ads_read'];
    $loginUrl = $helper->getLoginUrl('http://sparnorddashboard.local:8080/fb_callback.php', $permissions);

    echo '<a href="'.  $loginUrl .'">Log ind</a>';

?>

Callback

<?php
include_once __DIR__ . '/includes/connect.php';
require_once __DIR__ . '/vendor/autoload.php';

use Facebook\Facebook;

session_start();

$facebook = new Facebook([
    'app_id' => 'XXX',
    'app_secret' => 'XXX',
    'default_graph_version' => 'v2.5',
    ]);

$helper = $facebook->getRedirectLoginHelper();

try {

    $access_token = $helper->getAccessToken();


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

if(!isset($access_token)) {

    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;   

}

$oAuth2Client = $facebook->getOAuth2Client();
$tokenMetaData = $oAuth2Client->debugToken($access_token);

$tokenMetaData->validateAppId("1694068770828491");
$tokenMetaData->validateExpiration();


if(!$access_token->isLongLived()) {

    try {

        $access_token = $oAuth2Client->getLongLivedAccessToken($access_token);

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

        $e->getMessage();
        exit;

    }

}

$_SESSION['fb_access_token'] = (string) $access_token;

header('Location: http://sparnorddashboard.local:8080/fetchads.php?callback=callback');

?>

But this will use the user client token - How can I set it up so that I can us the app access token? I don't want a user to login and accept permissions, as this is for a internal dashboard, that runs on a TV moniter, that will read ads data - nothing else.


Solution

  • You can't use your APP access token for ads_read. If you don't want to get it via user login, then another way is to get via Graph Api Explorer.

    Assuming you already created an Ad Account :

    1. Login to Graph Api Explorer.
    2. Change to your App.
      enter image description here

    3. Click 'Get Token' and then 'Get User Access Token'
      enter image description here

    4. Click 'Extended Permissions' tab and check ads_read and click Get Access Token.
      enter image description here

    5. Grant yourself the permissions you requested.
      enter image description here

    6. Use the user access token that will be presented to you in the access token form input field:
      enter image description here


    Use the generated access token for your internal dashboard app/script, e.g:

    // Add to header of your file
    use FacebookAds\Api;
    
    // Initialize a new Session and instantiate an Api object
    Api::init(
      '{your-app-id}',
      '{your-app-secret}',
      $_SESSION['facebook_access_token'] // The generated access token
    );