Switching Access Tokens

In this example, we are getting a list of all a User's Pages using a User access token. Then, we are getting the Insights from the first Page on the list using that Page's access token.

// 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>);

To learn more about access tokens visit our Access Token Guide