diff --git a/add_to_fedilist.php b/add_to_fedilist.php index df246b0d..d9910859 100644 --- a/add_to_fedilist.php +++ b/add_to_fedilist.php @@ -35,9 +35,17 @@ function addVideoToFediList($videoUrl) { echo "‼️ Error: token.json missing or invalid. Authenticate first.\n"; 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)) { diff --git a/update_google_token.php b/update_google_token.php index f183070a..d6fe5412 100755 --- a/update_google_token.php +++ b/update_google_token.php @@ -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"); }