Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FB App This method must be called with a Page Access Token

When i use this code:

<?php 

require_once "vendor/autoload.php";

$config = ...;

use FacebookAds\Api;
use FacebookAds\Object\Page;

Api::init(
    $config['facebook']['app_id'], //APP_ID
    $config['facebook']['app_secret'], //APP SECRET
    $config['facebook']['app_access_token'] //Token generated by https://developers.facebook.com/tools/explorer for app
);

$page = new Page($config['facebook']['page_id']);
$leadgen_forms = $page->getLeadgenForms(); //heres an error

I get error: Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: (#190) This method must be called with a Page Access Token in ...

But when I put page_access_token in place of app_access_token (from https://developers.facebook.com/tools/explorer) i get error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid appsecret_proof provided in the API argument in .... When i remove line:

like image 977
unbreak Avatar asked Mar 09 '18 13:03

unbreak


2 Answers

Seems you are working on lead-gen forms that are meant for pages only. Your profile must have admin/developer role assigned. You definitely seem to have missed/copied incorrect value for one of the below. Below details are copied from https://developers.facebook.com/docs/marketing-api/guides/lead-ads/retrieving for quicker understanding

One can read leads or real-time updates by:

Using a Page Access Token, i.e. the Page admin's access token for the page. Page access token also allows you to read ad specific fields such as ad_id, campaign_id, etc., if you have atleast advertiser level permissions on the ad account associated with the lead ad.

Using User Access Token belonging to the page admin. To access all of the lead data and the ad level data, the access token should have manage_pages and ads_management scope.

You can manage user rights with Page roles. In addition, if you need to allow leads download for user with non-admin role on the page, you can whitelist it with leadgen_whitelisted_users endpoint.

like image 156
Rajesh Avatar answered Sep 28 '22 03:09

Rajesh


The other answers are not showing how to actually send a Page Access Token instead of an App Access Token or User Access Token.

require_once "vendor/autoload.php";

$config = ...;

use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Session;

$api = Api::init(
    $config['facebook']['app_id'], //APP_ID
    $config['facebook']['app_secret'], //APP SECRET
    $config['facebook']['app_access_token'] //Token generated by https://developers.facebook.com/tools/explorer for app
);
$page_api = $api->getCopyWithSession(new Session(
    $config['facebook']['app_id'], //APP_ID
    $config['facebook']['app_secret'], //APP SECRET
    $page_access_token  // <-- You can get this by accessing 'me/accounts' w/ the initial API
));
$page = new Page($config['facebook']['page_id'], null, $page_api); // <-- use the api with the Page Access Token here
$leadgen_forms = $page->getLeadgenForms(); //heres an error
like image 24
knsheely Avatar answered Sep 28 '22 03:09

knsheely