protected function FacebookPostCollector::defaultAccessToken in Social Feed 8
Generate the Facebook access token.
Return value
string The access token.
Throws
\GuzzleHttp\Exception\GuzzleException
1 call to FacebookPostCollector::defaultAccessToken()
- FacebookPostCollector::setFacebookClient in src/
Services/ FacebookPostCollector.php - Set the Facebook client.
File
- src/
Services/ FacebookPostCollector.php, line 173
Class
- FacebookPostCollector
- Class FacebookPostCollector.
Namespace
Drupal\socialfeed\ServicesCode
protected function defaultAccessToken() {
$config = \Drupal::service('config.factory')
->getEditable('socialfeed.facebooksettings');
$permanent_token = $config
->get('page_permanent_token');
if (empty($permanent_token)) {
$args = [
'usertoken' => $this->userToken,
'appid' => $this->appId,
'appsecret' => $this->appSecret,
'pageid' => $this->pageName,
];
$client = \Drupal::httpClient();
// Token.
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/oauth/access_token?grant_type=fb_exchange_token&client_id={$args['appid']}&client_secret={$args['appsecret']}&fb_exchange_token={$args['usertoken']}");
$request = json_decode($request
->getBody()
->getContents());
$long_token = $request->access_token;
// User ID.
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/me?access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
$account_id = $request->id;
// Page ID.
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/{$args['pageid']}?fields=id&access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
$page_id = $request->id;
// Permanent Token.
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/{$account_id}/accounts?access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
foreach ($request->data as $response_data) {
if ($response_data->id == $page_id) {
$config
->set('page_permanent_token', $response_data->access_token)
->save();
return $response_data->access_token;
}
}
}
else {
return $permanent_token;
}
}