Exemplo n.º 1
0
 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]);
 }
Exemplo n.º 2
0
 /**
  * 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';
     }
 }
Exemplo n.º 3
0
 /**
  * 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);
 }