[英]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. 我不希望用户登录并接受权限,因为这是针对内部仪表板的,该仪表板在电视监视器上运行,该仪表板将读取广告数据-别无其他。
You can't use your APP access token for ads_read
. 您不能将您的APP访问令牌用于ads_read
。 If you don't want to get it via user login, then another way is to get via Graph Api Explorer. 如果您不想通过用户登录获取它,那么另一种方法是通过Graph Api Explorer获取。
Assuming you already created an Ad Account : 假设您已经创建了一个广告帐户 :
Click 'Get Token' and then 'Get User Access Token' 点击“获取令牌”,然后点击“获取用户访问令牌”
Click 'Extended Permissions' tab and check ads_read
and click Get Access Token. 点击“扩展权限”标签,然后检查ads_read
并点击获取访问令牌。
Use the user access token that will be presented to you in the access token form input field: 使用将在访问令牌表单输入字段中显示给您的用户访问令牌:
Use the generated access token for your internal dashboard app/script, eg: 将生成的访问令牌用于内部仪表板应用程序/脚本,例如:
// 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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.