This document explains how to get a list of Page access tokens for Facebook Pages a person using your app manages.
The following code example, exchanges a User access token for a Page access token. Using the User access token, a list of all Pages the User manages is returned. This list includes Page access tokens for each Page. You can then use the Page access token to get information about the Page, such as page_fan_adds
insights.
These Page access tokens are valid for 1 hour.
// First we try to get all pages from a user
$fields = array(
'access_token',
);
$params = array(
);
$pages = (new User(<USER_ID>))->getAccounts(
$fields,
$params
);
$page_id = $pages[0]->id;
echo $pages[0]->id.PHP_EOL;
// Then we get first page's insight using page access token
$api = Api::init($app_id, null, $pages[0]->access_token);
$fields = array(
);
$params = array(
'metric' => 'page_fan_adds',
);
$insights = (new Page($page_id))->getInsights(
$fields,
$params
);
$api = Api::init($app_id, null, <ACCESS_TOKEN>);
# First we try to get all pages from a user
fields = [
'access_token',
]
params = {
}
pages = User(<USER_ID>).get_accounts(
fields=fields,
params=params,
)
page_id = pages[0].get_id()
print(pages[0].get_id())
# Then we get first page's insight using page access token
FacebookAdsApi.init(<ACCESS_TOKEN>=pages[0]["access_token"])
fields = [
]
params = {
'metric': 'page_fan_adds',
}
insights = Page(page_id).get_insights(
fields=fields,
params=params,
)
FacebookAdsApi.init(<ACCESS_TOKEN>=access_token)
// First we try to get all pages from a user
APINodeList<Page> pages = new User(<USER_ID>, context).getAccounts()
.requestAccessTokenField()
.execute();
String page_id = pages.get(0).getId();
System.out.println(pages.get(0).getFieldId());
// Then we get first page's insight using page access token
context = new APIContext(pages.get(0).getFieldAccessToken()).enableDebug(false);
APINodeList<InsightsResult> insights = new Page(page_id, context).getInsights()
.setMetric("page_fan_adds")
.execute();
context = new APIContext(<ACCESS_TOKEN>).enableDebug(false);
# First we try to get all pages from a user
user = FacebookAds::User.get(<USER_ID>)
pages = user.accounts({
})
page_id = pages[0].id
print 'page_id:', page_id
print pages[0].id
# Then we get first page's insight using page access token
FacebookAds::Session.default_session.access_token = pages[0].access_token
page = FacebookAds::Page.get(page_id)
insights = page.insights({
metric: 'page_fan_adds',
})
FacebookAds::Session.default_session.access_token = <ACCESS_TOKEN>
// First we try to get all pages from a user
fields = [
'<ACCESS_TOKEN>',
];
params = {
};
let pages = await (new User(<USER_ID>)).getAccounts(
fields,
params
);
let page_id = pages[0].id;
console.log(pages[0].id);
// Then we get first page's insight using page access token
bizSdk.FacebookAdsApi.init(pages[0].<ACCESS_TOKEN>);
fields = [
];
params = {
'metric' : 'page_fan_adds',
};
let insights = await (new Page(page_id)).getInsights(
fields,
params
);
bizSdk.FacebookAdsApi.init(<ACCESS_TOKEN>);