Sends a request for an access token.
@param $hapiClient	The client used to send the request.
@throws HttpException
356888
private function getAccessToken(Http\HapiClient $hapiClient)
    {
        $urlEncodedBody = new Http\UrlEncodedBody([
            'grant_type' => $this->grantType,
            'scope' => $this->scope
        ]);
        
        $basic = base64_encode($this->userid . ':' . $this->password);
        $authorizationHeader = [
            'Accept' => 'application/json',
            'Authorization' => 'Basic ' . $basic
        ];
        
        $request = new Http\Request(
            $this->tokenEndPointUrl,
            'POST',
            null,
            $urlEncodedBody,
            $authorizationHeader
        );
        
        // Send the request
        $state = $hapiClient->sendRequest($request)->getState();
        
        // Check the response
        if (!isset($state['access_token']) || !isset($state['expires_in'])) {
            throw new \Exception('The authentication was a success but the response did not contain the token or its validity limit.');
        }
        
        // We update the token
        $this->token = new ExpirableToken($state['access_token'], $this->getTime() + $state['expires_in']);
    }
686848
protected function sendRequest($method, $endpoint, $params = [], $accessToken = null)
    {
        $client = $this->getNewHttpClient($method, $params, $accessToken);
        $response = $client->request($method, $endpoint);
        $responseBody = $response->getBody()->getContents();

        return $this->getReturnJson() ? $responseBody : json_decode($responseBody, true);
    }
842649
public function handle(RequestInterface $request): RequestInterface
    {
        if ($this->token === '' || $this->key === '') {
            throw new AuthorizationException('Could not create api token, missing or empty arguments');
        }
        return $request->withHeader($this->key, $this->token);
    }
1054455
protected function sendRequest()
    {
        try {
            $this->response = $this->client->send($this->request);
        } catch (RequestException $e) {
            $this->response = $e->getResponse();

            if (null === $this->response) {
                if ($this->throwsRequestException) {
                    throw $e;
                }
            }

            if ($this->throwsRequestException) {
                throw new \Exception((string)$this->response->getBody());
            }
        }
    }
661173
protected function requestAccessToken($grantType = 'client_credential')
    {
        $result = $this->httpGet(self::WECHAT_ACCESS_TOKEN_PREFIX, [
            'appid' => $this->appId,
            'secret' => $this->appSecret,
            'grant_type' => $grantType
        ]);
        return isset($result['access_token']) ? $result : false;
    }
312250
public function execute($request)
    {
        RequestNotSupportedException::assertSupports($this, $request);


        $details = ArrayObject::ensureArrayObject($request->getModel());

        /** if no payment type provided in config, execute a choose payment type action
         so that user can choose the payment type*/
        if ($details[PayBoxRequestParams::PBX_TYPEPAIEMENT] == null
            && $this->api->getOptions()['type_paiement'] == null) {
            $choosePaymentTypeRequest = new ChoosePaymentType($details);
            $this->gateway->execute($choosePaymentTypeRequest);
        }

        $httpRequest = new GetHttpRequest();
        $this->gateway->execute($httpRequest);

        if (isset($httpRequest->query['error_code'])) {
            $details->replace($httpRequest->query);
        } else {
            $this->api->doPayment((array)$details);
        }
    }
454286
private function setBasicApiClient()
    {
        $this->client = new Client();

        // if using a self issued access token,
        // just authenticate the user right now
        if (!is_null($this->accessToken)) {
            $this->client->authenticate($this->accessToken);
        }
    }
466955
public function sendRequest()
    {
        if ($this->getError()->hasError()) {
            return;
        }

        if (class_exists($this->dispatcher)) {
            $dispatcherClass = $this->dispatcher;
            $dispatcher = new $dispatcherClass($this->instance->getQuery());
            if ($dispatcher instanceof AbstractDispatcher) {
                try {
                    $client = new \SoapCLient($this->getUrl(), array('trace' => 1));

                    $config = $this->config->getRawConfig();
                    $sql = $this->instance->getQuery()->toString();
                    $limit = $this->instance->getQuery()->getLimit();
                    $offset = $this->instance->getQuery()->getOffset();
                    $single = $this->instance->getQuery()->isSingle();

                    if ($this->method == 'getAvailableDrivers') {
                        $this->availableDrivers = $client->getAvailableDrivers();
                    } elseif ($this->method == 'getServerVersion') {
                        $res = $client->getServerVersion();
                        $this->serverVersion = isset($res['version']) ? $res['version'] : null;
                    } else {
                        $response = $client->getResult($config, $sql, $limit, $offset, $single, $this->getKey(), 'array');
                        $dispatcher->dispatch($response);
                    }
                } catch (\SoapFault $e) {
                    $this->getError()->setMessage($e->getMessage());
                } catch (\Exception $e) {
                    $this->getError()->setMessage('Error server response');
                }
            } else {
                $this->getError()->setMessage(sprintf(
                    'Dispatcher class must be instance of %s, %s given',
                    AbstractDispatcher::class,
                    (string) $this->dispatcher
                ));
            }
        } else {
            $this->getError()->setMessage(sprintf('Class %s not found', (string) $this->dispatcher));
        }
    }
558543
public function setHttpClient(HttpClient $client)
    {
        $this->httpClient = $client;

        $this->httpClient->setDefaultOption(
            'headers/Authorization',
            $this->getAuthorizationHeader()
        );
        $this->httpClient->setDefaultOption(
            'headers/Accepts',
            'application/json'
        );

        $this->bootModules();
    }
867615
public function create(Connect $connect)
    {
        $tokenRequest = $this->getTokenizer()->signTokenRequest(
            $this->getTokenizer()->createTokenRequest(
                $connect->getUser(),
                $connect->getSaml()->getMetadata()->getServiceProvider()->getEntityID()
            ),
            $connect->getSaml()->getPrivateKey()
        );

        $request = (new RequestDescriptor())
            ->setUrl($this->buildUrl('/api/token'))
            ->setMethod('POST');
        $request->setBodyParams(['token-request' => json_encode($tokenRequest->toArray())]);

        try {
            return json_decode($this->send($request)->getBody(), true);
        } catch (\Exception $e) {
            $previous = $e->getPrevious();

            if ($previous instanceof BadResponseException) {
                $error = json_decode($previous->getResponse()->getBody(true), true);

                throw new TokenException($error['error'], $error['code'], $previous);
            }

            throw new TokenException($e->getMessage(), $e->getCode(), $e->getPrevious());
        }
    }
949682
protected function send(RequestInterface $request)
    {
        try {
            $response = $this->httpClient->sendRequest($request);

            $body = (string) $response->getBody();

            return \json_decode($body, true);
        } catch (\Exception $e) {
            throw new ErrorException(sprintf('There was an error requesting Paylands API: %s', $e->getMessage()));
        }
    }
44692
public function beforeApiRequestSend($event)
    {
        $accessToken = $this->getAccessToken();
        if (!is_object($accessToken) || !$accessToken->getIsValid()) {
            throw new Exception('Invalid access token.');
        }

        $this->applyAccessTokenToRequest($event->request, $accessToken);
    }
409778
public function requestAccessToken($code)
    {
        $client = $this->getClient();

        try {
            $response = $client->request(
                'POST',
                '/token',
                [
                    'auth' => [
                        $this->clientId,
                        $this->clientSecret
                    ],
                    'form_params' => [
                        'grant_type'    => 'authorization_code',
                        'code'          => $code,
                        'client_id'     => $this->clientId,
                        'client_secret' => $this->clientSecret
                    ]
                ]
            );
        } catch (ClientException $ex) {
            $result = $this->getDecodedBody($ex->getResponse()->getBody());

            if (is_array($result) && isset($result['error'])) {
                // handle a service error message
                $message = 'Service responsed with error code "' . $result['error'] . '".';

                if (isset($result['error_description']) && $result['error_description']) {
                    $message .= ' Description "' . $result['error_description'] . '".';
                }
                throw new AuthRequestException($message, 0, $ex);
            }

            // unknown error. not parsed error
            throw $ex;
        }

        try {
            $result = $this->getDecodedBody($response->getBody());
        } catch (\RuntimeException $ex) {
            throw new AuthResponseException('Server response can\'t be parsed', 0, $ex);
        }

        if (!is_array($result)) {
            throw new AuthResponseException('Server response has unknown format');
        }

        if (!isset($result['access_token'])) {
            throw new AuthResponseException('Server response doesn\'t contain access token');
        }

        $this->setAccessToken($result['access_token']);

        $lifetimeInSeconds = $result['expires_in'];

        $expireDateTime = new \DateTime();
        $expireDateTime->add(new \DateInterval('PT'.$lifetimeInSeconds.'S'));

        $this->setExpiresIn($expireDateTime);

        return $this;
    }
453258
public function setAccessToken($accessToken, $type = null, $expires = null)
    {
        if (is_string($accessToken)) {
            $accessToken = new AccessToken($accessToken, $type, ['expires' => $expires]);
        } elseif (!$accessToken instanceof AccessToken) {
            throw new \InvalidArgumentException('Invalid access token');
        }
        $this->accessToken = $accessToken;
        $this->refreshToken = $accessToken->getRefreshToken();
    }
513208
public function setAccessToken($access_token)
    {
        if (!is_string($access_token)) {
            throw new InvalidArgumentException("Invalid AccessToken type, need string.");
        }
        $cache = $this->config->get('appid') . '_access_token';
        Tools::setCache($cache, $this->access_token = $access_token);
    }
580687
public function requestAccessToken()
    {
        try {
            $this->session->requestAccessToken($_GET['code']);
            return $this;
        } catch (Exception $e) {
            $this->redirectToSpotifyAuthorizeUrl();
        }
    }
832292
public function send()
    {

        if (headers_sent()) {
            throw new HttpException(Response::STATUS_INTERNAL_SERVER_ERROR, 'Headers already send');
        }

        header($this->getStatusLine());

        foreach ($this->headers as $header) {
            header($header);
        }

        echo $this->body;
    }