Пример #1
0
 /**
  * @param string $resource
  * @param string $method
  * @param array $params
  */
 protected function setUri($resource, $method, array $params)
 {
     $query = array('access_token' => $this->oauth2Client->getAccessToken());
     if ('GET' === $method || 'HEAD' === $method) {
         $query = array_merge($query, $params);
     }
     $this->request->setUri($this->configuration->getBaseUri() . $resource . '?' . $this->httpClient->buildQuery($query));
 }
Пример #2
0
 /**
  * call an api endpoint. automatically adds needed authorization headers with access token or parameters
  *
  * @param string $endpoint
  * @param string $method default 'GET'
  * @param array $uriParameters optional
  * @param mixed $postBody optional, can be string or array
  * @param array $additionalHeaders
  * @return string
  */
 public function callApiEndpoint($endpoint, $method = 'GET', array $uriParameters = array(), $postBody = null, array $additionalHeaders = array())
 {
     if (!$this->_dataStore->retrieveAccessToken()->getAccessToken()) {
         throw new Exception('could not revoke access token, no access token found, did you forgot call autorize()!!');
     }
     $token = $this->_dataStore->retrieveAccessToken();
     //check if token is invalid
     if ($token->getLifeTime() && $token->getLifeTime() < time()) {
         $token = $this->refreshAccessToken($token);
     }
     $parameters = null;
     $authorizationMethod = $this->_configuration->getAuthorizationMethod();
     switch ($authorizationMethod) {
         case Service\Configuration::AUTHORIZATION_METHOD_HEADER:
             $additionalHeaders = array_merge(array('Authorization: OAuth ' . $token->getAccessToken()), $additionalHeaders);
             break;
         case Service\Configuration::AUTHORIZATION_METHOD_ALTERNATIVE:
             if ($method !== 'GET') {
                 if (is_array($postBody)) {
                     $postBody['oauth_token'] = $token->getAccessToken();
                 } else {
                     $postBody .= '&oauth_token=' . urlencode($token->getAccessToken());
                 }
             } else {
                 $uriParameters['oauth_token'] = $token->getAccessToken();
             }
             break;
         default:
             throw new Exception("Invalid authorization method specified");
             break;
     }
     if ($method !== 'GET') {
         if (is_array($postBody)) {
             $parameters = http_build_query($postBody);
         } else {
             $parameters = $postBody;
         }
     }
     if (!empty($uriParameters)) {
         $endpoint .= (strpos($endpoint, '?') !== false ? '&' : '?') . http_build_query($uriParameters);
     }
     $http = new HttpClient($endpoint, $method, $parameters, $additionalHeaders);
     $http->execute();
     return $http->getResponse();
 }
Пример #3
0
 /**
  * Return an access token.
  *
  * @throws \RuntimeException
  * @return string|null
  */
 public function getAccessToken()
 {
     $accessToken = $this->getAccessTokenFromPersistence();
     $grandType = $this->configuration->getGrantType();
     if (null !== $accessToken) {
         if ($grandType !== $this->storage->get(self::GRAND_TYPE_KEY, null)) {
             throw new \RuntimeException('grand_type switching not supported');
         }
         return $accessToken;
     }
     if ('password' === $grandType) {
         $response = $this->requestAccessToken();
     } else {
         throw new \RuntimeException('invalid grand_type configured');
     }
     if (null === $response || 200 != $response->getStatusCode()) {
         return null;
     }
     return $this->setAccessTokenToPersistence($response);
 }