summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2018-01-24 11:48:36 +0100
committeremkael <emkael@tlen.pl>2018-01-24 11:48:36 +0100
commit16a32604f5f2459418dd94f31de499990f0dfb69 (patch)
treeb635a7ec6d83c5abcb3881bcacbafc6e98f9df0d
parent76dc5c4be53a6eda2e45fec3d9fe64030877a8cd (diff)
Tweaks to FB token auto-renewal
-rw-r--r--_cron/fb-user-token2
-rw-r--r--bin/fb-user-token-expiry1
-rw-r--r--bin/fb-user-token.php55
-rwxr-xr-xbin/fb-user-token.sh3
-rw-r--r--config/facebook.jsonbin336 -> 329 bytes
5 files changed, 34 insertions, 27 deletions
diff --git a/_cron/fb-user-token b/_cron/fb-user-token
index 007c9c7..e07d3c1 100644
--- a/_cron/fb-user-token
+++ b/_cron/fb-user-token
@@ -1 +1 @@
-25 9 4,14,24 * * $SITEPATH/bin/fb-user-token.sh
+25 * * * * $SITEPATH/bin/fb-user-token.sh
diff --git a/bin/fb-user-token-expiry b/bin/fb-user-token-expiry
new file mode 100644
index 0000000..a523532
--- /dev/null
+++ b/bin/fb-user-token-expiry
@@ -0,0 +1 @@
+2018-03-25 \ No newline at end of file
diff --git a/bin/fb-user-token.php b/bin/fb-user-token.php
index 943bf4c..3aeb8aa 100644
--- a/bin/fb-user-token.php
+++ b/bin/fb-user-token.php
@@ -3,29 +3,36 @@
$basePath = dirname(__FILE__);
$configFile = $basePath . '/../config/facebook.json';
-$config = json_decode(file_get_contents($configFile), TRUE);
-$firstLink =
- 'https://graph.facebook.com/oauth/client_code?' .
- http_build_query([
- 'access_token' => $config['user_token'],
- 'client_id' => $config['app_id'],
- 'client_secret' => $config['app_secret'],
- 'redirect_uri' => 'http://rss.emkael.info/facebook.php'
- ]);
-$firstResponse = json_decode(file_get_contents($firstLink));
-
-$secondLink =
- 'https://graph.facebook.com/oauth/access_token?' .
- http_build_query([
- 'client_id' => $config['app_id'],
- 'code' => $firstResponse->code,
- 'redirect_uri' => 'http://rss.emkael.info/facebook.php'
- ]);
-$secondResponse = json_decode(file_get_contents($secondLink));
-
-$config['user_token'] = $secondResponse->access_token;
-
-print json_encode($config);
-fwrite(STDERR, 'FB API token renewed, expires on: ' . date('Y-m-d H:i:s', strtotime('+' . $secondResponse->expires_in . ' seconds')) . PHP_EOL);
+$expiryFile = $basePath . '/fb-user-token-expiry';
+
+if (!file_exists($expiryFile) || trim(file_get_contents($expiryFile)) == date('Y-m-d')) {
+
+ $config = json_decode(file_get_contents($configFile), TRUE);
+ $firstLink =
+ 'https://graph.facebook.com/oauth/client_code?' .
+ http_build_query([
+ 'access_token' => $config['user_token'],
+ 'client_id' => $config['app_id'],
+ 'client_secret' => $config['app_secret'],
+ 'redirect_uri' => 'http://rss.emkael.info/facebook.php'
+ ]);
+ $firstResponse = json_decode(file_get_contents($firstLink));
+
+ $secondLink =
+ 'https://graph.facebook.com/oauth/access_token?' .
+ http_build_query([
+ 'client_id' => $config['app_id'],
+ 'code' => $firstResponse->code,
+ 'redirect_uri' => 'http://rss.emkael.info/facebook.php'
+ ]);
+ $secondResponse = json_decode(file_get_contents($secondLink));
+
+ $config['user_token'] = $secondResponse->access_token;
+
+ fwrite(STDERR, 'FB API token renewed, expires on: ' . date('Y-m-d H:i:s', strtotime('+' . $secondResponse->expires_in . ' seconds')) . PHP_EOL);
+ file_put_contents($configFile, json_encode($config));
+ file_put_contents($expiryFile, date('Y-m-d', strtotime('+' . $secondResponse->expires_in . ' seconds')));
+
+}
?>
diff --git a/bin/fb-user-token.sh b/bin/fb-user-token.sh
index c0ad09f..396e20a 100755
--- a/bin/fb-user-token.sh
+++ b/bin/fb-user-token.sh
@@ -1,4 +1,3 @@
#!/bin/bash
cd /srv/websites/rss.emkael.info
-php bin/fb-user-token.php | json_pp > config/temp.json
-mv config/temp.json config/facebook.json
+php bin/fb-user-token.php
diff --git a/config/facebook.json b/config/facebook.json
index 3c2fdea..b3f6aee 100644
--- a/config/facebook.json
+++ b/config/facebook.json
Binary files differ