renamed scripts
This commit is contained in:
parent
2be0f2c5b3
commit
e9bc45815c
1 changed files with 0 additions and 0 deletions
130
update_google_token.php
Executable file
130
update_google_token.php
Executable file
|
@ -0,0 +1,130 @@
|
|||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
// === 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'];
|
||||
|
||||
// === 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";
|
||||
return;
|
||||
}
|
||||
|
||||
$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']);
|
||||
} 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");
|
||||
}
|
||||
|
||||
echo "==== DEVICE AUTHORIZATION ====\n";
|
||||
echo "Visit: " . $deviceData['verification_url'] . "\n";
|
||||
echo "Enter code: " . $deviceData['user_code'] . "\n\n";
|
||||
|
||||
echo "Waiting...\n";
|
||||
|
||||
|
||||
// === Step 2: Poll for token ===
|
||||
$token = null;
|
||||
$startTime = time();
|
||||
while (true) {
|
||||
sleep($deviceData['interval']);
|
||||
|
||||
$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'])) {
|
||||
$token = $tokenResponse;
|
||||
echo "Saving token.json\n";
|
||||
file_put_contents(__DIR__ . '/_credentials/token.json', json_encode($token));
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($tokenResponse['error']) && $tokenResponse['error'] !== 'authorization_pending') {
|
||||
die("Auth error: " . $tokenResponse['error'] . "\n");
|
||||
}
|
||||
|
||||
if (time() - $startTime > $deviceData['expires_in']) {
|
||||
die("Authorization timed out.\n");
|
||||
}
|
||||
}
|
||||
|
||||
// === Step 3: Create FediList Playlist ===
|
||||
createPlaylist($token['access_token']);
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue