diff --git a/app/Commands/TraktLogin.php b/app/Commands/TraktLogin.php new file mode 100644 index 0000000..592080a --- /dev/null +++ b/app/Commands/TraktLogin.php @@ -0,0 +1,55 @@ + 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; + + } +} diff --git a/app/Commands/TraktRefreshLogin.php b/app/Commands/TraktRefreshLogin.php new file mode 100644 index 0000000..b17b3c4 --- /dev/null +++ b/app/Commands/TraktRefreshLogin.php @@ -0,0 +1,45 @@ +line("Failed to get OAuth response"); + return static::FAILURE; + } + + $data = [ + 'refresh_token' => $oauth_resp['refresh_token'], + 'client_id' => env('TRAKT_APP_ID'), + 'client_secret' => env('TRAKT_APP_SECRET'), + 'redirect_uri' => env('TRAKT_REDIRECT_URI'), + 'grant_type' => 'refresh_token', + ]; + + $resp = $trakt->request()->post('/oauth/token', $data); + + if (!$resp->ok()) { + $this->line('Failed to get new token'); + return static::FAILURE; + } + + // $this->line(json_encode($resp->body())); + // $body = $json->body(); + $body = $resp->json(); + $this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'"); + $this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']); + + return static::SUCCESS; + } +} diff --git a/app/Services/Trakt.php b/app/Services/Trakt.php index 9958eda..7d4b736 100644 --- a/app/Services/Trakt.php +++ b/app/Services/Trakt.php @@ -26,6 +26,11 @@ class Trakt return Http::withHeaders($this->headers + $headers)->baseUrl('https://api.trakt.tv'); } + public function requestNoAuth(array $headers = []): PendingRequest + { + return Http::withHeaders(['trakt-api-version' => 2] + $headers)->baseUrl('https://api.trakt.tv'); + } + public function syncHistory(WatchData $data, OutputInterface $output = null): Response { $output?->writeln(sprintf('Submitting %s to trakt with headers %s', json_encode($data->structuredData), json_encode($this->headers)));