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
|
<?php
|
||||||
|
|
||||||
|
|
||||||
// === Example use ===
|
// === Example use ===
|
||||||
#addVideoToFediList('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
#addVideoToFediList('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
||||||
|
|
||||||
|
require_once('utils.php');
|
||||||
|
|
||||||
function isYouTubeLink($url) {
|
function isYouTubeLink($url) {
|
||||||
return preg_match('#^(https?://)?(www\.)?(youtube\.com/watch\?v=|youtu\.be/)[a-zA-Z0-9_-]{11}#', $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);
|
//print_r($token);
|
||||||
|
|
||||||
// === Refresh token if expired ===
|
// === 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'])) {
|
if (isset($token['expires_at'])) {
|
||||||
$secondsLeft = $token['expires_at'] - time();
|
$secondsLeft = $token['expires_at'] - time();
|
||||||
if ($secondsLeft > 0) {
|
if ($secondsLeft > $refreshSecondMargin) {
|
||||||
$minutes = floor($secondsLeft / 60);
|
$minutes = floor($secondsLeft / 60);
|
||||||
$seconds = $secondsLeft % 60;
|
$seconds = $secondsLeft % 60;
|
||||||
echo "⏳ Token expires in $minutes minutes and $seconds seconds.\n";
|
echo "⏳ Token expires in $minutes minutes and $seconds seconds.\n";
|
||||||
} else {
|
} 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', [
|
$refreshResponse = curlPost('https://oauth2.googleapis.com/token', [
|
||||||
'client_id' => $clientId,
|
'client_id' => $clientId,
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/php
|
#!/usr/bin/php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once('utils.php');
|
||||||
|
|
||||||
// === Load client credentials ===
|
// === Load client credentials ===
|
||||||
$secrets = json_decode(file_get_contents(__DIR__ . '/_credentials/client_secret.json'), true);
|
$secrets = json_decode(file_get_contents(__DIR__ . '/_credentials/client_secret.json'), true);
|
||||||
$client = $secrets['installed'] ?? $secrets['web'] ?? null;
|
$client = $secrets['installed'] ?? $secrets['web'] ?? null;
|
||||||
|
@ -12,31 +14,13 @@ if (!$client || !isset($client['client_id'], $client['client_secret'])) {
|
||||||
$clientId = $client['client_id'];
|
$clientId = $client['client_id'];
|
||||||
$clientSecret = $client['client_secret'];
|
$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 ===
|
// === Create playlist helper ===
|
||||||
function createPlaylist($accessToken) {
|
function createPlaylist($accessToken) {
|
||||||
|
|
||||||
$path = __DIR__ . '/_credentials/fedilist_id.txt';
|
$path = __DIR__ . '/_credentials/fedilist_id.txt';
|
||||||
|
|
||||||
if (file_exists($path)) {
|
if (file_exists($path)) {
|
||||||
echo "FediList Playlist ID already exists at :$path\n";
|
echo "✅ FediList Playlist ID already exists at :$path\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,11 +72,15 @@ if (!isset($deviceData['user_code'])) {
|
||||||
die("Failed to get device code.\n");
|
die("Failed to get device code.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
echo "==============================\n";
|
||||||
echo "==== DEVICE AUTHORIZATION ====\n";
|
echo "==== DEVICE AUTHORIZATION ====\n";
|
||||||
|
echo "==============================\n";
|
||||||
echo "Visit: " . $deviceData['verification_url'] . "\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 ===
|
// === Step 2: Poll for token ===
|
||||||
|
@ -100,6 +88,7 @@ $token = null;
|
||||||
$startTime = time();
|
$startTime = time();
|
||||||
while (true) {
|
while (true) {
|
||||||
sleep($deviceData['interval']);
|
sleep($deviceData['interval']);
|
||||||
|
echo ".";
|
||||||
|
|
||||||
$tokenResponse = curlPost('https://oauth2.googleapis.com/token', [
|
$tokenResponse = curlPost('https://oauth2.googleapis.com/token', [
|
||||||
'client_id' => $clientId,
|
'client_id' => $clientId,
|
||||||
|
@ -110,22 +99,23 @@ while (true) {
|
||||||
|
|
||||||
if (isset($tokenResponse['access_token'])) {
|
if (isset($tokenResponse['access_token'])) {
|
||||||
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
|
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
|
||||||
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($tokenResponse));
|
$path = __DIR__ . '/_credentials/token.json';
|
||||||
echo "✅ Token saved with expiration.\n";
|
file_put_contents($path, json_encode($tokenResponse));
|
||||||
|
echo "\n✅ Token saved as $path.\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("\nAuth error: " . $tokenResponse['error'] . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (time() - $startTime > $deviceData['expires_in']) {
|
if (time() - $startTime > $deviceData['expires_in']) {
|
||||||
die("Authorization timed out.\n");
|
die("\nAuthorization timed out.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Step 3: Create FediList Playlist ===
|
// === 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