我正在使用Facebook Ads API,对此我是全新的。提取广告帐户时出现错误。错误是Invalid OAuth access token.
收到此错误后,我来到Google并进行了类似的搜索
First Link Another one another one。但是没有找到任何解决方案,所以我决定来Stackoverflow ,所以这个问题不再重复
这是我如何使用Facebook Ads API的代码
public function login() {
$helper = $this->facebook->getRedirectLoginHelper();
$this->request->session()->write('fb_access_token', false);
$access_token = $this->request->session()->read('fb_access_token');
if ($access_token) {
echo "You are logged in!";
} else {
$permissions = ['email', 'ads_management', 'pages_show_list', 'publish_pages', 'manage_pages', 'ads_read'];
$loginUrl = $helper->getLoginUrl('http://localhost/FacebookManager/Adsmanagement/authenticate', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
}
}
public function authenticate() {
if (isset($_GET['state'])) {
$helper = $this->facebook->getRedirectLoginHelper();
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
$accessToken = $helper->getAccessToken();
if($accessToken) {
echo "got the token";
$this->request->session()->write('fb_access_token', $accessToken);
//echo $accessToken;
$oAuth2Client = $this->facebook->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$this->facebook->setDefaultAccessToken($longLivedAccessToken);
}
}
}
我正在获得这样的帐户
public function getAdsAccount() {
$accesstoken = $this->request->session()->read('fb_access_token');
//$accesstoken = "AQwfmuNgKZCZCiZBuhAMGB8xZBty5dgNR7J9ZBO2ZCDl1SWYxnBLn9ahIn5FQTWxkJ0FPKbemeG0vUAwPsZCN2FFxOFey9BQr8CZBW07IwlZB0FK81SH8ozOil2GllR35BCyxQGhQ7JrtuCdNE6kjEOU0FZCAZDZD";
Api::init(
$this->app_id, // App ID
$this->appsecret,
$accesstoken // Your user access token
);
//*****************
$account = new AdAccount('****************');
$contracts = $account->read();
echo "<pre>"; print_r($contracts->getData());
}
因此,我正在使用开发人员帐户,尝试通过Facebook的测试用户从APP仪表板->角色->测试用户中获取此帐户。我正在设置适当的权限等。不知道我在哪里错了。
更新 现在的问题是,是否可以从Graph Explorar创建访问令牌,但是可以,如果我使用SDK创建,则会给我错误“无效的Tokan Tokan”
请帮助我。提前致谢。
答案 0 :(得分:0)
您好@manoj Kumar,请使用下面的URL(具有管理员登录名)进行检查,以调试AccessToken
。
https://developers.facebook.com/tools/explorer/
请检查以下来自Facebook PHP SDK的代码,以获取具有权限ads_management
的有效访问令牌。
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
// Init PHP Sessions
session_start();
$fb = new Facebook([
'app_id' => '{your-app-id}',
'app_secret' => '{your-app-secret}',
]);
$helper = $fb->getRedirectLoginHelper();
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!";
} else {
$permissions = ['ads_management'];
$loginUrl = $helper->getLoginUrl('http://localhost:8888/marketing-api/', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
}
之后,您必须获取AdAccount,因此请使用以下代码
use FacebookAds\Object\AdUser;
// Add after Api::init()
$me = new AdUser('me');
$my_adaccount = $me->getAdAccounts()->current();
答案 1 :(得分:0)
这实际上是我的错误。 Facebook API向我们返回一个保护对象。如果您打印变量,它将显示为字符串。
<?php
echo $longLivedAccessToken;
var_dump($longLivedAccessToken);
?>
我尝试了$longLivedAccessToken->getValue();
$oAuth2Client = $this->facebook->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$accesstoken = $longLivedAccessToken->getValue();
$this->request->session()->write('fb_access_token', $accesstoken);
$this->facebook->setDefaultAccessToken($longLivedAccessToken);