cleaner login process
This commit is contained in:
parent
dee2522676
commit
920c8932a0
3 changed files with 47 additions and 28 deletions
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
|
||||
|
||||
// === Example use ===
|
||||
#addVideoToFediList('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
||||
|
||||
require_once('utils.php');
|
||||
|
||||
function isYouTubeLink($url) {
|
||||
return preg_match('#^(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[a-zA-Z0-9_-]{11}#', $url);
|
||||
}
|
||||
|
@ -39,14 +42,19 @@ function addVideoToFediList($videoUrl) {
|
|||
//print_r($token);
|
||||
|
||||
// === Refresh token if expired ===
|
||||
|
||||
// Don't let it expire, refresh if we have less than 15 minutes left
|
||||
// Google Tokens usually last 60 min, so more ore less around 45min we get a new one
|
||||
$refreshSecondMargin = 15*60;
|
||||
|
||||
if (isset($token['expires_at'])) {
|
||||
$secondsLeft = $token['expires_at'] - time();
|
||||
if ($secondsLeft > 0) {
|
||||
if ($secondsLeft > $refreshSecondMargin) {
|
||||
$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 or will expire in less than $refreshSecondMargin seconds. (Seconds Left: $secondsLeft). Refreshing...\n";
|
||||
|
||||
$refreshResponse = curlPost('https://oauth2.googleapis.com/token', [
|
||||
'client_id' => $clientId,
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
require_once('utils.php');
|
||||
|
||||
// === Load client credentials ===
|
||||
$secrets = json_decode(file_get_contents(__DIR__ . '/_credentials/client_secret.json'), true);
|
||||
$client = $secrets['installed'] ?? $secrets['web'] ?? null;
|
||||
|
@ -12,31 +14,13 @@ if (!$client || !isset($client['client_id'], $client['client_secret'])) {
|
|||
$clientId = $client['client_id'];
|
||||
$clientSecret = $client['client_secret'];
|
||||
|
||||
// === cURL helper function ===
|
||||
function curlPost($url, $data, $headers = []) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
'User-Agent: curl/7.64.1'
|
||||
], $headers));
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
$response = curl_exec($ch);
|
||||
if (curl_errno($ch)) {
|
||||
die("cURL error: " . curl_error($ch) . "\n");
|
||||
}
|
||||
curl_close($ch);
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
// === Create playlist helper ===
|
||||
function createPlaylist($accessToken) {
|
||||
|
||||
$path = __DIR__ . '/_credentials/fedilist_id.txt';
|
||||
|
||||
if (file_exists($path)) {
|
||||
echo "FediList Playlist ID already exists at :$path\n";
|
||||
echo "✅ FediList Playlist ID already exists at :$path\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -88,11 +72,15 @@ if (!isset($deviceData['user_code'])) {
|
|||
die("Failed to get device code.\n");
|
||||
}
|
||||
|
||||
echo "\n";
|
||||
echo "==============================\n";
|
||||
echo "==== DEVICE AUTHORIZATION ====\n";
|
||||
echo "==============================\n";
|
||||
echo "Visit: " . $deviceData['verification_url'] . "\n";
|
||||
echo "Enter code: " . $deviceData['user_code'] . "\n\n";
|
||||
echo "\n";
|
||||
echo "Enter code: " . $deviceData['user_code'] . "\n\nFinish login process and come back here.";
|
||||
|
||||
echo "Waiting...\n";
|
||||
echo "\n\nWaiting...";
|
||||
|
||||
|
||||
// === Step 2: Poll for token ===
|
||||
|
@ -100,6 +88,7 @@ $token = null;
|
|||
$startTime = time();
|
||||
while (true) {
|
||||
sleep($deviceData['interval']);
|
||||
echo ".";
|
||||
|
||||
$tokenResponse = curlPost('https://oauth2.googleapis.com/token', [
|
||||
'client_id' => $clientId,
|
||||
|
@ -110,22 +99,23 @@ while (true) {
|
|||
|
||||
if (isset($tokenResponse['access_token'])) {
|
||||
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
|
||||
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse));
|
||||
echo "✅ Token saved with expiration.\n";
|
||||
$path = __DIR__ . '/_credentials/token.json';
|
||||
file_put_contents($path, json_encode($tokenResponse));
|
||||
echo "\n✅ Token saved as $path.\n";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
|
||||
die("Auth error: " . $tokenResponse['error'] . "\n");
|
||||
die("\nAuth error: " . $tokenResponse['error'] . "\n");
|
||||
}
|
||||
|
||||
if (time() - $startTime > $deviceData['expires_in']) {
|
||||
die("Authorization timed out.\n");
|
||||
die("\nAuthorization timed out.\n");
|
||||
}
|
||||
}
|
||||
|
||||
// === Step 3: Create FediList Playlist ===
|
||||
createPlaylist($token['access_token']);
|
||||
createPlaylist($tokenResponse['access_token']);
|
||||
|
||||
?>
|
||||
|
|
21
utils.php
Normal file
21
utils.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
// === cURL helper function ===
|
||||
function curlPost($url, $data, $headers = []) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
'User-Agent: curl/7.64.1'
|
||||
], $headers));
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
$response = curl_exec($ch);
|
||||
if (curl_errno($ch)) {
|
||||
die("cURL error: " . curl_error($ch) . "\n");
|
||||
}
|
||||
curl_close($ch);
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue