Skip to content

Commit

Permalink
update generating linkedin access token
Browse files Browse the repository at this point in the history
  • Loading branch information
seibtph authored and [email protected] committed Oct 8, 2021
1 parent 9dd8ab7 commit 4d31540
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
13 changes: 9 additions & 4 deletions src/Controller/LinkedinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ public function authAction(Request $request): Response
'redirect_uri' => $this->router->generate('auth_linkedin', [], RouterInterface::ABSOLUTE_URL)
];

$token = json_decode(file_get_contents('https://www.linkedin.com/oauth/v2/accessToken?'.http_build_query($data)));
try {
$token = json_decode(file_get_contents('https://www.linkedin.com/oauth/v2/accessToken?'.http_build_query($data)));

// Store the access token and remove temporary session key
$this->db->update('tl_social_feed', ['linkedin_access_token' => $token->access_token, 'access_token_expires' => time() + $token->expires_in, 'linkedin_refresh_token' => $token->refresh_token, 'linkedin_refresh_token_expires' => time() + $token->refresh_token_expires_in], ['id' => $sessionData['socialFeedId']]);
$this->session->remove(SocialFeedListener::SESSION_KEY);
} catch(Exception $e) {
\System::log($e->getMessage(), __METHOD__, TL_GENERAL);
}

// Store the access token and remove temporary session key
$this->db->update('tl_social_feed', ['linkedin_access_token' => $token->access_token, 'access_token_expires' => time() + $token->expires_in, 'linkedin_refresh_token' => $token->refresh_token, 'linkedin_refresh_token_expires' => time() + $token->refresh_token_expires_in], ['id' => $sessionData['socialFeedId']]);
$this->session->remove(SocialFeedListener::SESSION_KEY);
}

return new RedirectResponse($sessionData['backUrl']);
Expand Down
31 changes: 20 additions & 11 deletions src/EventListener/CronListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ private function getFbAccountPicture($fb, $accessToken, $account) {
public function getLinkedinPosts() {
$objSocialFeed = SocialFeedModel::findAll();

\System::log('getLinkedinPosts', __METHOD__, TL_GENERAL);

if(NULL === $objSocialFeed)
{
return;
Expand All @@ -312,7 +310,7 @@ public function getLinkedinPosts() {
$client->setAccessToken( $obj->linkedin_access_token);

$posts = $client->get(
'shares?q=owners&owners=urn:li:organization:'.$obj->linkedin_company_id
'shares?q=owners&owners=urn:li:organization:'.$obj->linkedin_company_id.'&sharesPerOwner='.$obj->number_posts
);

$organization = $client->get(
Expand Down Expand Up @@ -616,11 +614,18 @@ public function refreshLinkedInAccessToken() {
return;
}

$this->import('Database');

foreach($objSocialFeed as $obj) {

if ($obj->socialFeedType == "LinkedIn") {
if ($obj->socialFeedType == "LinkedIn" && $obj->linkedin_access_token != "" && $obj->linkedin_refresh_token != "") {
if($obj->linkedin_refresh_token_expires <= strtotime("+1 week", time())) {
$objMail = new \Email();
$objMail->subject = 'LinkedIn Access Token Erinnerung';
$objMail->html = 'Hallo Admin, <br><br>Der LinkedIn Access Token auf der Webseite '.$this->Environment->httpHost.' für den Account '.$obj->linkedin_company_id.' muss neu generiert werden. Melde dich dafür im Contao Backend an, rufe die Einstellungen des Social Feed Accounts auf, wähle die Checkbox "Generiere Access Token" aus und speichere. Anschließend musst du nur noch der App den Zugriff erlauben und der Access Token wird neu generiert. Dieser Vorgang muss jedes Jahr wiederholt werden.';
$objMail->from = $GLOBALS['TL_CONFIG']['adminEmail'];
$objMail->fromName = $this->Environment->httpHost;
$objMail->sendTo($GLOBALS['TL_CONFIG']['adminEmail']);
}

if($obj->access_token_expires <= strtotime("+1 week", time())) {
$data = [
'grant_type' => 'refresh_token',
Expand All @@ -629,12 +634,16 @@ public function refreshLinkedInAccessToken() {
'client_secret' => $obj->linkedin_client_secret
];

$token = json_decode(file_get_contents('https://www.linkedin.com/oauth/v2/accessToken?'.http_build_query($data)));
try {
$token = json_decode(file_get_contents('https://www.linkedin.com/oauth/v2/accessToken?'.http_build_query($data)));

// Store the access token
$db = \Contao\Database::getInstance();
$set = ['linkedin_access_token' => $token->access_token, 'access_token_expires' => time() + $token->expires_in, 'linkedin_refresh_token' => $token->refresh_token, 'linkedin_refresh_token_expires' => time() + $token->refresh_token_expires_in];
$db->prepare('UPDATE tl_social_feed %s WHERE id = ?')->set($set)->execute($obj->id);
// Store the access token
$db = \Contao\Database::getInstance();
$set = ['linkedin_access_token' => $token->access_token, 'access_token_expires' => time() + $token->expires_in, 'linkedin_refresh_token' => $token->refresh_token, 'linkedin_refresh_token_expires' => time() + $token->refresh_token_expires_in];
$db->prepare('UPDATE tl_social_feed %s WHERE id = ?')->set($set)->execute($obj->id);
} catch(Exception $e) {
\System::log($e->getMessage(), __METHOD__, TL_GENERAL);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Resources/contao/dca/tl_social_feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
'socialFeedType_Facebook' => 'pdir_sf_fb_account,pdir_sf_fb_app_id,pdir_sf_fb_app_secret,pdir_sf_fb_access_token,psf_facebookRequestToken,pdir_sf_fb_news_archive,pdir_sf_fb_news_cronjob,pdir_sf_fb_posts,pdir_sf_fb_news_last_import_date,pdir_sf_fb_news_last_import_time',
'socialFeedType_Instagram' => 'psf_instagramAppId,psf_instagramAppSecret,psf_instagramAccessToken,psf_instagramRequestToken,instagram_account,number_posts,pdir_sf_fb_news_archive,pdir_sf_fb_news_cronjob,pdir_sf_fb_news_last_import_date,pdir_sf_fb_news_last_import_time;{pdir_sf_account_image_legend},instagram_account_picture,instagram_account_picture_size',
'socialFeedType_Twitter' => 'twitter_api_key,twitter_api_secret_key,twitter_access_token,twitter_access_token_secret,twitter_account,search,number_posts,pdir_sf_fb_news_archive,pdir_sf_fb_news_cronjob,show_retweets,hashtags_link,show_reply,pdir_sf_fb_news_last_import_date,pdir_sf_fb_news_last_import_time',
'socialFeedType_LinkedIn' => 'linkedin_client_id,linkedin_client_secret,linkedin_company_id,linkedin_request_token,linkedin_access_token,access_token_expires,number_posts,pdir_sf_fb_news_archive,pdir_sf_fb_news_cronjob,pdir_sf_fb_news_last_import_date,pdir_sf_fb_news_last_import_time,linkedin_account_picture,linkedin_account_picture_size'
'socialFeedType_LinkedIn' => 'linkedin_client_id,linkedin_client_secret,linkedin_company_id,linkedin_request_token,linkedin_access_token,number_posts,pdir_sf_fb_news_archive,pdir_sf_fb_news_cronjob,access_token_expires,linkedin_refresh_token_expires,pdir_sf_fb_news_last_import_date,pdir_sf_fb_news_last_import_time,linkedin_account_picture,linkedin_account_picture_size'
),

'fields' => [
Expand Down Expand Up @@ -444,6 +444,7 @@
'exclude' => true,
'inputType' => 'text',
'eval' => [
'mandatory' => true,
'maxlength' => 255,
'tl_class' => 'w50'
],
Expand Down Expand Up @@ -526,7 +527,7 @@
'inputType' => 'text',
'sql' => "varchar(255) NOT NULL default ''",
'eval' => [
'rgxp' => 'date',
'rgxp' => 'datim',
'tl_class' => 'w50',
'readonly' => 'readonly'
]
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/contao/languages/de/tl_social_feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_request_token'] = array("Generiere Access Token","Wenn Sie die Checkbox setzen und anschließend Speichern wird der Access Token generiert.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture'] = array("Profilbild","Wählen Sie hier ein Profilbild aus, welches auf der Webseite angezeigt wird.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture_size'] = array("Bildgröße","Hier können Sie die Abmessungen des Bildes und den Skalierungsmodus festlegen.");
$GLOBALS['TL_LANG']['tl_social_feed']['access_token_expires'] = array("Access Token läuft ab","Date on which the access token expires.");
$GLOBALS['TL_LANG']['tl_social_feed']['access_token_expires'] = array("Access Token läuft ab am","Solange der Refresh Token gültig ist verlängert sich der Access Token automatisch um jeweils 2 Monate.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_refresh_token_expires'] = array("Refresh Token läuft ab am","Wenn der Refresh Token abgelaufen ist muss der Access Token manuell neu generiert werden.");
3 changes: 2 additions & 1 deletion src/Resources/contao/languages/en/tl_social_feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_request_token'] = array("Generate Access Token","Check this box and save the record to request the access token.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture'] = array("Account picture","Here you can choose a account picture which will displayed on the website.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture_size'] = array("Image size","Here you can set the image dimensions and the resize mode.");
$GLOBALS['TL_LANG']['tl_social_feed']['access_token_expires'] = array("Access token expires in","");
$GLOBALS['TL_LANG']['tl_social_feed']['access_token_expires'] = array("Access token expires in","As long as the refresh token is valid, the access token is automatically extended by 2 months at a time.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_refresh_token_expires'] = array("Refresh token expires in","If the refresh token has expired, the access token must be regenerated manually.");
11 changes: 11 additions & 0 deletions src/Resources/contao/languages/it/tl_social_feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_app_id'] = array("App ID","Inserisci qui l'ID app della tua app Facebook.");
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_app_secret'] = array("App Secret","Inserisci la chiave segreta della tua app di Facebook.");
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_access_token'] = array("Access Token","Inserisci il token di accesso. Le istruzioni per la generazione del token di accesso sono disponibili nella documentazione.");
$GLOBALS['TL_LANG']['tl_social_feed']['psf_facebookRequestToken'] = array("Genera token di accesso","Quando salvi, il token di accesso viene generato automaticamente.");
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_news_archive'] = ['Archivio News','Seleziona l\'archivio delle news dove devono essere importati i post.'];
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_news_cronjob'] = ['Esecuzione del cron job','Inserisci la frequenza con cui deve essere richiamato il cron job per importare i post.'];
$GLOBALS['TL_LANG']['tl_social_feed']['pdir_sf_fb_news_last_import_date'] = array("Ultima importazione (data): verrà compilata automaticamente","Questo campo viene compilato automaticamente e non deve essere compilato.");
Expand Down Expand Up @@ -41,4 +42,14 @@
$GLOBALS['TL_LANG']['tl_social_feed']['twitter_api_secret_key'] = array("API Secret Key","Inserisci qui la chiave segreta dell'API.");
$GLOBALS['TL_LANG']['tl_social_feed']['twitter_access_token'] = array("Access Token","Inserisci qui il token di accesso.");
$GLOBALS['TL_LANG']['tl_social_feed']['twitter_access_token_secret'] = array("Access Token Secret","Inserisci qui l\'Access Token Secret.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_client_id'] = array("Client ID","Inserisci qui l'ID cliente dall'app.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_client_secret'] = array("Client Secret","Inserisci qui il client secret dall'app.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_company_id'] = array("ID sito aziendale","Inserisci qui l'ID del sito aziendale.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_access_token'] = array("Access Token","Il token di accesso viene impostato automaticamente dopo aver salvato e impostato la casella di controllo.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_request_token'] = array("Genera access token","Se selezioni la casella e poi salvi, viene generato il token di accesso.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture'] = array("Immagine del profilo","Seleziona qui un'immagine del profilo, che verrà visualizzata sul sito web.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_account_picture_size'] = array("Dimensione dell'immagine","
Qui puoi impostare le dimensioni dell'immagine e la modalità di ridimensionamento.");
$GLOBALS['TL_LANG']['tl_social_feed']['access_token_expires'] = array("Access Token scade il","Finché il token di aggiornamento è valido, il token di accesso viene automaticamente esteso di 2 mesi alla volta.");
$GLOBALS['TL_LANG']['tl_social_feed']['linkedin_refresh_token_expires'] = array("Refresh Token scade il","Se il token di aggiornamento è scaduto, il token di accesso deve essere rigenerato manualmente.");

0 comments on commit 4d31540

Please sign in to comment.