5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Facades\Http;
9 class TwitchToken extends Model
13 public function getAuthUrl() {
14 return 'https://id.twitch.tv/oauth2/authorize'
15 .'?response_type=code'
16 .'&client_id='.rawurlencode(config('twitch.client_id'))
17 .'&redirect_uri='.rawurlencode(config('twitch.redirect_uri'))
18 .'&scope='.implode('+', array_map('rawurlencode', $this->scope));
21 public function refresh() {
22 $rsp = Http::post('https://id.twitch.tv/oauth2/token', [
23 'client_id' => config('twitch.client_id'),
24 'client_secret' => config('twitch.client_secret'),
25 'grant_type' => 'refresh_token',
26 'refresh_token' => $this->refresh,
28 if (!$rsp->successful()) {
29 throw new \Exception($rsp['message']);
31 $this->type = $rsp['token_type'];
32 $this->scope = $rsp['scope'];
33 $this->access = $rsp['access_token'];
34 $this->refresh = $rsp['refresh_token'];
38 public function request() {
39 return Http::baseUrl('https://api.twitch.tv/helix')
40 ->withToken($this->access)
42 'Client-Id' => config('twitch.client_id'),