handles Google refresh token

This commit is contained in:
Santiago Lema 2025-05-21 02:36:26 +00:00
parent b5adf7dbd0
commit dee2522676
2 changed files with 16 additions and 5 deletions

View file

@ -36,8 +36,16 @@ function addVideoToFediList($videoUrl) {
return false;
}
//print_r($token);
// === Refresh token if expired ===
if (isset($token['expires_at']) && time() >= $token['expires_at']) {
if (isset($token['expires_at'])) {
$secondsLeft = $token['expires_at'] - time();
if ($secondsLeft > 0) {
$minutes = floor($secondsLeft / 60);
$seconds = $secondsLeft % 60;
echo "⏳ Token expires in $minutes minutes and $seconds seconds.\n";
} else {
echo "🔄 Access token expired. Refreshing...\n";
$refreshResponse = curlPost('https://oauth2.googleapis.com/token', [
@ -58,6 +66,8 @@ function addVideoToFediList($videoUrl) {
return false;
}
}
}
// === Extract video ID ===
if (!preg_match('/(?:v=|\/)([a-zA-Z0-9_-]{11})/', $videoUrl, $matches)) {

View file

@ -109,12 +109,13 @@ while (true) {
]);
if (isset($tokenResponse['access_token'])) {
$token = $tokenResponse;
echo "Saving token.json\n";
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($token));
break;
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse));
echo "✅ Token saved with expiration.\n";
break;
}
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
die("Auth error: " . $tokenResponse['error'] . "\n");
}