/**
  * Make an api request.
  * 
  * @param  string $url
  * @param  string $httpMethod The http method to use with this request
  * @param  string $data Optional data to send with request
  * @param  string $contentType
  * @return array The json parsed response from the server
  */
 private function api($url, $httpMethod = 'GET', $data = null, $contentType = null)
 {
     // trigger an event
     $this->trigger('api_request_init', array('url' => $url));
     $this->authenticate();
     $accessToken = $this->auth->getAccessToken();
     if (!$accessToken) {
         // should have a token at this point
         // if not, something went wrong
         throw new \Exception('Cannot make an api request without an access token');
     }
     // save this request in case we need to use the refresh token
     $lastRequest = array('url' => $url, 'method' => $httpMethod, 'data' => $data);
     // add the access token onto the url
     $url = $url . '?access_token=' . $accessToken;
     $this->trigger('api_request_send', array('url' => $url, 'http_method' => $httpMethod, 'data' => $data));
     $this->request->setMethod($httpMethod)->setData($data);
     if ($contentType) {
         $this->request->setContentType("application/{$contentType}");
     }
     // now send the request
     $this->request->send($url);
     $this->trigger('api_request_complete', array('url' => $url, 'response' => $this->request->getResponse()));
     // check the response for any errors
     $vaild = $this->checkResponse();
     if (!$vaild && $this->isTokenExpired) {
         // blow out the current token so a new one gets requested
         $this->auth->clearAccessToken();
         // redo the last api request
         $this->api($lastRequest['url'], $lastRequest['method'], $lastRequest['data']);
     }
     return $this->request->getResponse();
 }
 /**
  * Get a new access token with a refresh token.
  * 
  * @param  string $refreshToken 
  * @return array|boolean Array of token data returned from the auth server or false on error
  */
 public function requestTokenWithRefreshToken($refreshToken)
 {
     $this->trigger('request_token_with_refresh_token', array('refresh_token' => $refreshToken));
     // use the refresh token to get a new access token
     $url = $this->getTokenUrl();
     $this->request->setMethod('post')->setData(array('client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'grant_type' => 'refresh_token', 'refresh_token' => $refreshToken))->send($url);
     $valid = $this->checkResponse();
     if ($valid) {
         $this->tokenData = $this->request->getResponse();
         $this->tokenData['expires_at'] = $this->tokenData['expires_in'] + time();
         $this->trigger('new_access_token', $this->tokenData);
         return $this->tokenData;
     }
     return false;
 }