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]);
 }
Exemple #2
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);
 }