Example #1
0
 public function createAuthUrl($scopes = '', $state = null)
 {
     $addy = '';
     if ($this->client->isForceRelogin() === true) {
         $addy .= '&force_verify=true';
     }
     if ($state) {
         $addy .= '&state=' . $state;
     }
     $url = sprintf('%s/oauth2/authorize/?response_type=code&client_id=%s&redirect_uri=%s&scope=%s%s', $this->client->getApiUrl(), $this->client->getClientId(), $this->client->getRedirectUri(), urlencode($scopes), $addy);
     return $url;
 }
Example #2
0
 public function sendRequest(HttpRequest $request)
 {
     if (!$this->client) {
         throw new Exception\TwitchException('Client was not set');
     }
     $apiurl = rtrim($this->client->getApiUrl(), '/');
     $url = sprintf('%s/%s', $apiurl, $request->getUrl());
     $url = rtrim($url, '/');
     $this->getAdapter()->open();
     $data = $this->getAdapter()->send($url, $request->getRequestMethod(), $request->getRequestHeaders(), $request->getPostBody());
     $status_code = $this->getAdapter()->getHttpStatusCode();
     $data = json_decode($data);
     $this->getAdapter()->close();
     // TODO we need to return more info
     if ($status_code !== 200) {
         throw new Exception\TwitchException($data->error, $data->status);
     }
     return $data;
 }
 /**
  * Call and process the 'virtual' method as defined in Client.php
  *
  * @param string $method
  * @param array $arguments
  * @param array $body
  * @return mixed
  * @throws Exception\AuthorizationRequiredException
  * @throws Exception\RuntimeException
  */
 public function call($method, $arguments = array(), $body = array())
 {
     if ($this->validate_arguments($method, $arguments) === true) {
         $headers = array();
         $headers['Accept'] = 'application/vnd.twitchtv.v3+json';
         $headers['Client-ID'] = $this->client->getClientId();
         if (isset($this->methods[$method]['requiresAuth']) === true) {
             if ($this->methods[$method]['requiresAuth'] === true && !$this->client->getAccessToken()) {
                 throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' requires authorization. Did you forget to use setAccessToken() ?');
             }
         }
         if (isset($this->methods[$method]['requireScope']) == false) {
             /* Should the be an auth exception ? */
             throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' did not set define any scope.');
         } else {
             $client_scope = $this->getClient()->getScope();
             $required_scope = $this->methods[$method]['requireScope'];
             foreach ($required_scope as $scope) {
                 if (in_array($scope, $client_scope) == false) {
                     throw new Exception\AuthorizationRequiredException('Method: ' . $method . ' requires scope: ' . $scope . ' to be set.');
                 }
             }
         }
         if ($this->client->getAccessToken()) {
             $headers['Authorization'] = 'OAuth ' . $this->client->getAccessToken();
         }
         if (isset($this->url_parts[$method]) === true and is_array($this->url_parts[$method]) === true) {
             if (count($this->url_parts[$method]) > 0) {
                 foreach ($this->url_parts[$method] as $key => $url_part) {
                     $this->methods[$method]['path'] = str_replace($key, $url_part['value'], $this->methods[$method]['path']);
                     unset($arguments[$url_part['name']]);
                 }
             }
         }
         $url = '/' . $this->methods[$method]['path'];
         $count = 0;
         while ($value = current($arguments)) {
             $url .= ($count > 0 ? '&' : '?') . key($arguments) . '=' . urlencode($value);
             next($arguments);
             $count++;
         }
         $request = new HttpRequest($url, $this->methods[$method]['httpMethod'], $headers, $body);
         $response = $this->client->getTransport()->sendRequest($request);
         return $response;
     }
 }