vid-queue/app/Commands/TraktLogin.php

55 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Commands;
use App\Services\Trakt;
class TraktLogin extends Command
{
protected $signature = 'trakt:login';
protected $description = 'Login and get new token.';
public function handle(Trakt $trakt): int
{
$data = [
'client_id' => env('TRAKT_APP_ID'),
];
$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 = [
'client_id' => env('TRAKT_APP_ID'),
'client_secret' => env('TRAKT_APP_SECRET'),
'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;
}
}