Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/Harvest.php
Строк: 193
<?php

namespace OAuthOAuth2Service;

use 
OAuthCommonConsumerCredentialsInterface;
use 
OAuthCommonHttpClientClientInterface;
use 
OAuthCommonHttpExceptionTokenResponseException;
use 
OAuthCommonHttpUriUri;
use 
OAuthCommonHttpUriUriInterface;
use 
OAuthCommonStorageTokenStorageInterface;
use 
OAuthCommonTokenTokenInterface;
use 
OAuthOAuth2TokenStdOAuth2Token;

class 
Harvest extends AbstractService
{

    public function 
__construct(
        
CredentialsInterface $credentials,
        
ClientInterface $httpClient,
        
TokenStorageInterface $storage,
        
$scopes = array(),
        
UriInterface $baseApiUri null
    
) {
        
parent::__construct($credentials$httpClient$storage$scopes$baseApiUri);

        if (
null === $baseApiUri) {
            
$this->baseApiUri = new Uri('https://api.harvestapp.com/');
        }
    }

    
/**
     * {@inheritdoc}
     */
    
public function getAuthorizationUri(array $additionalParameters = array())
    {
        
$parameters array_merge(
            
$additionalParameters,
            array(
                
'client_id'     => $this->credentials->getConsumerId(),
                
'redirect_uri'  => $this->credentials->getCallbackUrl(),
                
'state' => 'optional-csrf-token',
                
'response_type' => 'code',
            )
        );

        
// Build the url
        
$url = clone $this->getAuthorizationEndpoint();
        foreach (
$parameters as $key => $val) {
            
$url->addToQuery($key$val);
        }

        return 
$url;
    }

    
/**
     * {@inheritdoc}
     */
    
public function getAuthorizationEndpoint()
    {
        return new 
Uri('https://api.harvestapp.com/oauth2/authorize');
    }

    
/**
     * {@inheritdoc}
     */
    
public function getAccessTokenEndpoint()
    {
        return new 
Uri('https://api.harvestapp.com/oauth2/token');
    }

    
/**
     * {@inheritdoc}
     */
    
protected function getAuthorizationMethod()
    {
        return static::
AUTHORIZATION_METHOD_QUERY_STRING;
    }

    
/**
     * {@inheritdoc}
     */
    
protected function parseAccessTokenResponse($responseBody)
    {
        
$data json_decode($responseBodytrue);

        if (
null === $data || ! is_array($data)) {
            throw new 
TokenResponseException('Unable to parse response.');
        } elseif (isset(
$data['error'])) {
            throw new 
TokenResponseException('Error in retrieving token: "' $data['error'] . '"');
        }

        
$token = new StdOAuth2Token();
        
$token->setAccessToken($data['access_token']);
        
$token->setLifetime($data['expires_in']);
        
$token->setRefreshToken($data['refresh_token']);

        unset(
$data['access_token']);

        
$token->setExtraParams($data);

        return 
$token;
    }

    
/**
     * Refreshes an OAuth2 access token.
     *
     * @param TokenInterface $token
     *
     * @return TokenInterface $token
     *
     * @throws MissingRefreshTokenException
     */
    
public function refreshAccessToken(TokenInterface $token)
    {
        
$refreshToken $token->getRefreshToken();

        if (empty(
$refreshToken)) {
            throw new 
MissingRefreshTokenException();
        }

        
$parameters = array(
            
'grant_type'    => 'refresh_token',
            
'type'          => 'web_server',
            
'client_id'     => $this->credentials->getConsumerId(),
            
'client_secret' => $this->credentials->getConsumerSecret(),
            
'refresh_token' => $refreshToken,
        );

        
$responseBody $this->httpClient->retrieveResponse(
            
$this->getAccessTokenEndpoint(),
            
$parameters,
            
$this->getExtraOAuthHeaders()
        );
        
$token $this->parseAccessTokenResponse($responseBody);
        
$this->storage->storeAccessToken($this->service(), $token);

        return 
$token;
    }

    
/**
     * @return array
     */
    
protected function getExtraOAuthHeaders()
    {
        return array(
'Accept' => 'application/json');
    }

    
/**
     * Return any additional headers always needed for this service implementation's API calls.
     *
     * @return array
     */
    
protected function getExtraApiHeaders()
    {
        return array(
'Accept' => 'application/json');
    }
}
Онлайн: 2
Реклама