handles Google refresh token
This commit is contained in:
parent
b5adf7dbd0
commit
dee2522676
2 changed files with 16 additions and 5 deletions
|
@ -35,9 +35,17 @@ function addVideoToFediList($videoUrl) {
|
||||||
echo "‼️ Error: token.json missing or invalid. Authenticate first.\n";
|
echo "‼️ Error: token.json missing or invalid. Authenticate first.\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//print_r($token);
|
||||||
|
|
||||||
// === Refresh token if expired ===
|
// === 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";
|
echo "🔄 Access token expired. Refreshing...\n";
|
||||||
|
|
||||||
$refreshResponse = curlPost('https://oauth2.googleapis.com/token', [
|
$refreshResponse = curlPost('https://oauth2.googleapis.com/token', [
|
||||||
|
@ -58,6 +66,8 @@ function addVideoToFediList($videoUrl) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// === Extract video ID ===
|
// === Extract video ID ===
|
||||||
if (!preg_match('/(?:v=|\/)([a-zA-Z0-9_-]{11})/', $videoUrl, $matches)) {
|
if (!preg_match('/(?:v=|\/)([a-zA-Z0-9_-]{11})/', $videoUrl, $matches)) {
|
||||||
|
|
|
@ -109,12 +109,13 @@ while (true) {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (isset($tokenResponse['access_token'])) {
|
if (isset($tokenResponse['access_token'])) {
|
||||||
$token = $tokenResponse;
|
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
|
||||||
echo "Saving token.json\n";
|
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse));
|
||||||
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($token));
|
echo "✅ Token saved with expiration.\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
|
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
|
||||||
die("Auth error: " . $tokenResponse['error'] . "\n");
|
die("Auth error: " . $tokenResponse['error'] . "\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue