2025-04-07 11:05:40 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Services\Trakt;
|
2025-11-16 20:45:27 -06:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2025-04-07 11:05:40 -05:00
|
|
|
|
|
|
|
|
class TraktLogin extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = 'trakt:login';
|
|
|
|
|
protected $description = 'Login and get new token.';
|
2025-11-16 20:45:27 -06:00
|
|
|
public function handle(Trakt $trakt, Repository $config): int
|
2025-04-07 11:05:40 -05:00
|
|
|
{
|
|
|
|
|
$data = [
|
2025-11-16 20:45:27 -06:00
|
|
|
'client_id' => $config->get('trakt.app_id'),
|
2025-04-07 11:05:40 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$resp = $trakt->requestNoAuth()->post('/oauth/device/code', $data);
|
|
|
|
|
if (!$resp->ok()) {
|
|
|
|
|
$this->line('Failed to get device code: ' . $resp->status());
|
|
|
|
|
$this->line($resp->body());
|
|
|
|
|
return static::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$body = $resp->json();
|
|
|
|
|
$this->line("Visit {$body['verification_url']} and enter the code {$body['user_code']}");
|
|
|
|
|
if (!$this->confirm('Ready to continue?')) {
|
|
|
|
|
return static::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'");
|
|
|
|
|
|
|
|
|
|
$data = [
|
2025-11-16 20:45:27 -06:00
|
|
|
'client_id' => $config->get('trakt.app_id'),
|
|
|
|
|
'client_secret' => $config->get('trakt.app_secret'),
|
2025-04-07 11:05:40 -05:00
|
|
|
'code' => $body['device_code'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$resp = $trakt->requestNoAuth()->post('/oauth/device/token', $data);
|
|
|
|
|
|
|
|
|
|
if (!$resp->ok()) {
|
|
|
|
|
$this->line('Failed to get new token');
|
|
|
|
|
return static::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$body = $resp->json();
|
|
|
|
|
$this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'");
|
|
|
|
|
$this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']);
|
|
|
|
|
|
|
|
|
|
return static::SUCCESS;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|