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

2

6

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:

Katherinakatherine answered 9/3, 2018 at 13:49 Comment(2)
Show us where/how you “put page_access_token in place of app_access_token”Disregardful
@CBroe, I just copy from Explorer and paste it in config in place of app_access_token;Katherinakatherine
R
5

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.

Rescript answered 12/3, 2018 at 7:8 Comment(3)
How can I get these tokens?Katherinakatherine
Check it here developers.facebook.com/docs/marketing-api/accessRescript
I used different API objects for page and lead requests. I am still not sure why this is different now.Josuejosy
L
2

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
Leilaleilah answered 30/3, 2018 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.