Skip to content

Commit

Permalink
Update plugin with longlived token and refresh functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samzzi committed Apr 7, 2021
1 parent 0bb4a97 commit 5f602f2
Showing 1 changed file with 92 additions and 21 deletions.
113 changes: 92 additions & 21 deletions classes/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class Instagram
{
private $db;
private $token;
private $tokenRefreshed;

public function __construct()
{
Expand All @@ -24,6 +26,7 @@ public function __construct()

$this->db = kirby()->root('assets').DS.option('genxbe.instagram.assetFolder').DS.option('genxbe.instagram.db');
$this->token = site()->instagramToken();
$this->tokenRefreshed = page('sitesettings')->instagramTokenRefreshed();
}

public function feed()
Expand All @@ -33,32 +36,84 @@ public function feed()

public function getToken()
{
if(get('code'))
try
{
$options = [
'headers' => [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
],
'method' => 'POST',
'data' => http_build_query([
'client_id' => option('genxbe.instagram.client_id'),
'client_secret' => option('genxbe.instagram.client_secret'),
'grant_type' => 'authorization_code',
'redirect_uri' => option('genxbe.instagram.redirect_uri'),
'code' => get('code'),
]),
];

$data = \Remote::request('https://api.instagram.com/oauth/access_token', $options);
if(get('code'))
{
$options = [
'headers' => [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
],
'method' => 'POST',
'data' => http_build_query([
'client_id' => option('genxbe.instagram.client_id'),
'client_secret' => option('genxbe.instagram.client_secret'),
'grant_type' => 'authorization_code',
'redirect_uri' => option('genxbe.instagram.redirect_uri'),
'code' => get('code'),
]),
];

$data = \Remote::request('https://api.instagram.com/oauth/access_token', $options);

$access_token = $data->json()['access_token'];
$client_secret = option('genx.blocks-instagram.client_secret');

$data = \Remote::get("https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret={$client_secret}&access_token={$access_token}");

$access_token = $data->json()['access_token'];

site()->update([
'instagramAuth' => (boolean)true,
'instagramToken' => $data->json()['access_token'],
'instagramTokenRefreshed' => date('Y-m-d'),
]);

exec("php {__DIR__} fetch.php > /dev/null &");
}
}
catch(\Exception $e)
{
site()->update([
'instagramAuth' => (boolean)false,
'instagramToken' => '',
]);
}

go(site()->panelUrl(), 303);
}

public function refreshToken()
{
$kirby = kirby();
$kirby->impersonate('kirby');

try
{
if(site()->instagramAuth()->isFalse())
{
exit;
}

$access_token = $this->token;
$client_secret = option('genxbe.instagram.client_secret');

$data = \Remote::get("https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&client_secret={$client_secret}&access_token={$access_token}");

$access_token = $data->json()['access_token'];

site()->update([
'instagramAuth' => (boolean)true,
'instagramToken' => $data->json()['access_token'],
'instagramTokenRefreshed' => date('Y-m-d'),
]);
}
catch(\Exception $e)
{
site()->update([
'instagramAuth' => (boolean)false,
'instagramToken' => '',
]);

exec("php {__DIR__} fetch.php > /dev/null &");

go(site()->panelUrl(), 303);
}
}

Expand Down Expand Up @@ -121,7 +176,7 @@ public function fetch()
}
}

$newDb = $db->limit(20);
$newDb = $db->sortBy('timestamp', 'desc');

foreach(array_diff(scandir($path), array('.', '..')) as $file)
{
Expand All @@ -132,6 +187,22 @@ public function fetch()
}

$this->writeDb($newDb);

if($this->dateDifference(date('Y-m-d'), $this->tokenRefreshed) > 50)
{
self::refreshToken();
}
}

private function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);

$interval = date_diff($datetime1, $datetime2);

return $interval->format($differenceFormat);

}

private function getDb()
Expand Down

0 comments on commit 5f602f2

Please sign in to comment.