fedi_slurp/update_google_token.php

122 lines
3.4 KiB
PHP
Raw Normal View History

2025-05-21 00:44:19 +00:00
#!/usr/bin/php
<?php
2025-05-21 03:05:53 +00:00
require_once('utils.php');
2025-05-21 00:44:19 +00:00
// === Load client credentials ===
$secrets = json_decode(file_get_contents(__DIR__ . '/_credentials/client_secret.json'), true);
$client = $secrets['installed'] ?? $secrets['web'] ?? null;
if (!$client || !isset($client['client_id'], $client['client_secret'])) {
die("Error: Invalid client_secret.json format.\n");
}
$clientId = $client['client_id'];
$clientSecret = $client['client_secret'];
// === Create playlist helper ===
function createPlaylist($accessToken) {
$path = __DIR__ . '/_credentials/fedilist_id.txt';
if (file_exists($path)) {
2025-05-21 03:05:53 +00:00
echo "✅ FediList Playlist ID already exists at :$path\n";
return;
}
2025-05-21 00:44:19 +00:00
$data = [
'snippet' => [
'title' => 'FediList',
'description' => 'A custom playlist for federated videos'
],
'status' => [
'privacyStatus' => 'private'
]
];
$ch = curl_init('https://www.googleapis.com/youtube/v3/playlists?part=snippet,status');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $accessToken",
"Content-Type: application/json",
"User-Agent: curl/7.64.1"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
die("cURL error: " . curl_error($ch) . "\n");
}
curl_close($ch);
$result = json_decode($response, true);
$result = json_decode($response, true);
if (isset($result['id'])) {
echo "FediList created!\nPlaylist ID: " . $result['id'] . "\n";
file_put_contents($path, $result['id']);
2025-05-21 00:44:19 +00:00
} else {
echo "Failed to create playlist:\n$response\n";
}
}
// === Step 1: Get device code ===
$deviceData = curlPost('https://oauth2.googleapis.com/device/code', [
'client_id' => $clientId,
'scope' => 'https://www.googleapis.com/auth/youtube'
]);
if (!isset($deviceData['user_code'])) {
die("Failed to get device code.\n");
}
2025-05-21 03:05:53 +00:00
echo "\n";
echo "==============================\n";
2025-05-21 00:44:19 +00:00
echo "==== DEVICE AUTHORIZATION ====\n";
2025-05-21 03:05:53 +00:00
echo "==============================\n";
2025-05-21 00:44:19 +00:00
echo "Visit: " . $deviceData['verification_url'] . "\n";
2025-05-21 03:05:53 +00:00
echo "\n";
echo "Enter code: " . $deviceData['user_code'] . "\n\nFinish login process and come back here.";
2025-05-21 00:44:19 +00:00
2025-05-21 03:05:53 +00:00
echo "\n\nWaiting...";
2025-05-21 00:44:19 +00:00
// === Step 2: Poll for token ===
$token = null;
$startTime = time();
while (true) {
sleep($deviceData['interval']);
2025-05-21 03:05:53 +00:00
echo ".";
2025-05-21 00:44:19 +00:00
$tokenResponse = curlPost('https://oauth2.googleapis.com/token', [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'device_code' => $deviceData['device_code'],
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code'
]);
if (isset($tokenResponse['access_token'])) {
2025-05-21 02:36:26 +00:00
$tokenResponse['expires_at'] = time() + $tokenResponse['expires_in'];
2025-05-21 03:05:53 +00:00
$path = __DIR__ . '/_credentials/token.json';
file_put_contents($path, json_encode($tokenResponse));
echo "\n✅ Token saved as $path.\n";
2025-05-21 02:36:26 +00:00
break;
2025-05-21 00:44:19 +00:00
}
2025-05-21 02:36:26 +00:00
2025-05-21 00:44:19 +00:00
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
2025-05-21 03:05:53 +00:00
die("\nAuth error: " . $tokenResponse['error'] . "\n");
2025-05-21 00:44:19 +00:00
}
if (time() - $startTime > $deviceData['expires_in']) {
2025-05-21 03:05:53 +00:00
die("\nAuthorization timed out.\n");
2025-05-21 00:44:19 +00:00
}
}
// === Step 3: Create FediList Playlist ===
2025-05-21 03:05:53 +00:00
createPlaylist($tokenResponse['access_token']);
2025-05-21 00:44:19 +00:00
?>