使用app令牌访问Facebook应用

时间:2016-01-08 09:36:20

标签: php facebook facebook-graph-api token access-token

通过应用

访问某些广告数据时遇到一些问题

我有这两个脚本:

登录

<?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>';

?>

回调

<?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');

?>

但是这将使用用户客户端令牌 - 如何设置它以便我可以使用应用程序访问令牌?我不希望用户登录并接受权限,因为这是针对在电视监听器上运行的内部仪表板,它将读取广告数据 - 没有别的。

1 个答案:

答案 0 :(得分:1)

您无法使用ads_read的APP访问令牌。如果您不想通过用户登录获取,那么另一种方法是通过Graph Api Explorer获取。

假设您已经创建了Ad Account

  1. 登录Graph Api Explorer
  2. 更改为您的应用程序 enter image description here

  3. 点击&#39;获取令牌&#39;然后获取用户访问令牌&#39;
    enter image description here

  4. 点击“扩展权限”#39;选项卡并选中ads_read,然后点击获取访问令牌 enter image description here

  5. 授予您自己所要求的权限 enter image description here

  6. 使用将在访问令牌表单输入字段中显示的用户访问令牌:
    enter image description here

  7. 将生成的访问令牌用于内部仪表板应用程序/脚本,例如:

    // 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
    );