3 namespace App\Console\Commands;
5 use App\Models\TwitchToken;
6 use Illuminate\Console\Command;
7 use Illuminate\Support\Facades\Http;
9 class TwitchAuth extends Command {
12 * The name and signature of the console command.
16 protected $signature = 'twitch:auth {nick}';
19 * The console command description.
23 protected $description = 'Acquire a twitch oauth token';
26 * Execute the console command.
30 public function handle()
32 $token = TwitchToken::firstWhere('nick', $this->argument('nick'));
34 $token = new TwitchToken();
35 $token->nick = $this->argument('nick');
36 $token->scope = ['chat:read', 'chat:edit'];
38 $url = $token->getAuthUrl();
39 $this->line('Please visit '.$url);
40 $code = $this->ask('and enter the authorization code here');
41 $rsp = $this->fetchToken($code);
42 $token->type = $rsp['token_type'];
43 $token->access = $rsp['access_token'];
44 $token->refresh = $rsp['refresh_token'];
46 return Command::SUCCESS;
49 protected function fetchToken($code) {
50 $rsp = Http::post('https://id.twitch.tv/oauth2/token', [
51 'client_id' => config('twitch.client_id'),
52 'client_secret' => config('twitch.client_secret'),
54 'grant_type' => 'authorization_code',
55 'redirect_uri' => config('twitch.redirect_uri'),
57 if (!$rsp->successful()) {
58 throw new \Exception($rsp['message']);