我们目前正在尝试在Laravel中设置广告API。已经有几天了,但getAdAccounts()调用始终会导致“无效的OAuth访问令牌”。例外。 但是,在Graph Explorer API中使用相同的访问令牌对于adaccounts端点
也同样适用这是代码 -
try {
$fb = new Facebook([
env('FACEBOOK_APP_ID'), // App ID
env('FACEBOOK_APP_SECRET'),
env('FACEBOOK_GRAPH_VERSION')
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
if (!isset($_SESSION['facebook_access_token'])) {
$_SESSION['facebook_access_token'] = null;
}
if (!$_SESSION['facebook_access_token']) {
$helper = $fb->getRedirectLoginHelper();
try {
$_SESSION['facebook_access_token'] = (string) $helper->getAccessToken();
} catch(FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
if ($_SESSION['facebook_access_token']) {
echo "You are logged in!";
Api::init(
env('FACEBOOK_APP_ID'), // App ID
env('FACEBOOK_APP_SECRET'),
$_SESSION['facebook_access_token']
);
echo $_SESSION['facebook_access_token'];
$me = new AdUser('me');
//dd($me);
$my_adaccount = $me->getAdAccounts();
dd($my_adaccount);
} else {
$permissions = ['ads_management', 'user_friends'];
$loginUrl = $helper->getLoginUrl('http://localhost:8000/facebookads', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
}
}
catch(FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
catch(\Exception $e){
dd($e);
}
答案 0 :(得分:-1)
Api :: init(APP_ID,APP_SECRET,ACCESS_TOKEN); ACCESS_TOKEN不是引用$ _SESSION [&#39; facebooke_access_token&#39;]而是引用您的Marketing API令牌。 转到https://developers.facebook.com/apps/ / marketing-api / tools / 获取Marketing API令牌。