private function getClient() { $headers = ['Client-ID' => Twitch::getClientId(), 'Accept' => 'application/vnd.twitchtv.v' . Twitch::version() . '+json']; if (Twitch::getAccessToken()) { $headers['Authorization'] = 'Oauth ' . Twitch::getAccessToken(); } return new Client(['headers' => $headers, 'base_uri' => Twitch::baseURI]); }
public static function codeListener($url_parameters, $redirect_uri, $state = '') { if (empty($url_parameters['code'])) { return; } $post_data = ['client_id' => Twitch::getClientId(), 'client_secret' => Twitch::getClientSecret(), 'grant_type' => 'authorization_code', 'redirect_uri' => $redirect_uri, 'code' => $url_parameters['code']]; if (!empty($state)) { $post_data['state'] = $state; } return Twitch::api('oauth2/token')->post($post_data)->data(); }
/** * You obtain an access token when you follow the authentication process * https://github.com/justintv/Twitch-API/blob/master/authentication.md */ public static function setAccessToken($access_token, $client = null) { if (!is_string($access_token)) { throw new InvalidArgumentException("setAccessToken only accepts strings."); } static::$access_token = $access_token; $headers = ['Client-ID' => Twitch::getClientId(), 'Accept' => 'application/vnd.twitchtv.v' . Twitch::version() . '+json', 'Authorization' => 'Oauth ' . Twitch::getAccessToken()]; $client = $client ?: new \GuzzleHttp\Client(['headers' => $headers, 'base_uri' => Twitch::baseURI]); $request = $client->request('GET'); $access_token_validation = json_decode($request->getBody()); if (!$access_token_validation->token->valid) { throw new \Exception("This access token is not valid. Please confirm you have authorized the user correctly.", 401); } static::$scope = new Scope(); static::$scope->addScopes($access_token_validation->token->authorization->scopes); }