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(); }
/** * Providing no channel falls back to the /channel endpoint * * @param string $channel_name * @param mixed */ function __construct($channel_name = "", $client = null) { parent::__construct($client); $this->_channel = $channel_name; $this->_endpoint = $this->_base_endpoint = 'channels/' . $channel_name; if (empty($channel_name)) { if (empty(Twitch::getAccessToken())) { throw new ChannelException("Please provide a channel name, or use Twitch::setAccessToken() to define the channels access token."); } if (Twitch::$scope->isAuthorized('channel_read') === false) { throw new TwitchScopeException("You do not have sufficient scope priviledges to run this command. Make sure you're authorized for `channel_read`.", 401); } $this->_endpoint = 'channel'; } }
/** * 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); }
public function setAccessToken() { Twitch::setAccessToken('test_access_token', $this->getClient([new Response(200, [], file_get_contents(__DIR__ . '/data/root.json'))])); Twitch::$scope = new Scope(); }