[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/ -> Bitly.php (source)

   1  <?php
   2  
   3  namespace OAuth\OAuth2\Service;
   4  
   5  use OAuth\OAuth2\Token\StdOAuth2Token;
   6  use OAuth\Common\Http\Exception\TokenResponseException;
   7  use OAuth\Common\Http\Uri\Uri;
   8  use OAuth\Common\Consumer\CredentialsInterface;
   9  use OAuth\Common\Http\Client\ClientInterface;
  10  use OAuth\Common\Storage\TokenStorageInterface;
  11  use OAuth\Common\Http\Uri\UriInterface;
  12  
  13  class Bitly extends AbstractService
  14  {
  15      public function __construct(
  16          CredentialsInterface $credentials,
  17          ClientInterface $httpClient,
  18          TokenStorageInterface $storage,
  19          $scopes = array(),
  20          UriInterface $baseApiUri = null
  21      ) {
  22          parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  23  
  24          if (null === $baseApiUri) {
  25              $this->baseApiUri = new Uri('https://api-ssl.bitly.com/v3/');
  26          }
  27      }
  28  
  29      /**
  30       * {@inheritdoc}
  31       */
  32      public function getAuthorizationEndpoint()
  33      {
  34          return new Uri('https://bitly.com/oauth/authorize');
  35      }
  36  
  37      /**
  38       * {@inheritdoc}
  39       */
  40      public function getAccessTokenEndpoint()
  41      {
  42          return new Uri('https://api-ssl.bitly.com/oauth/access_token');
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      protected function getAuthorizationMethod()
  49      {
  50          return static::AUTHORIZATION_METHOD_QUERY_STRING;
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      protected function parseAccessTokenResponse($responseBody)
  57      {
  58          $data = json_decode($responseBody, true);
  59  
  60          if (null === $data || !is_array($data)) {
  61              throw new TokenResponseException('Unable to parse response.');
  62          } elseif (isset($data['error'])) {
  63              throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  64          }
  65  
  66          $token = new StdOAuth2Token();
  67          $token->setAccessToken($data['access_token']);
  68          // I'm invincible!!!
  69          $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
  70          unset($data['access_token']);
  71  
  72          $token->setExtraParams($data);
  73  
  74          return $token;
  75      }
  76  
  77      /**
  78       * {@inheritdoc}
  79       */
  80      public function requestAccessToken($code, $state = null)
  81      {
  82          if (null !== $state) {
  83              $this->validateAuthorizationState($state);
  84          }
  85  
  86          $bodyParams = array(
  87              'code'          => $code,
  88              'client_id'     => $this->credentials->getConsumerId(),
  89              'client_secret' => $this->credentials->getConsumerSecret(),
  90              'redirect_uri'  => $this->credentials->getCallbackUrl(),
  91              'grant_type'    => 'authorization_code',
  92          );
  93  
  94          $responseBody = $this->httpClient->retrieveResponse(
  95              $this->getAccessTokenEndpoint(),
  96              $bodyParams,
  97              $this->getExtraOAuthHeaders()
  98          );
  99  
 100          // we can scream what we want that we want bitly to return a json encoded string (format=json), but the
 101          // WOAH WATCH YOUR LANGUAGE ;) service doesn't seem to like screaming, hence we need to manually
 102          // parse the result
 103          $parsedResult = array();
 104          parse_str($responseBody, $parsedResult);
 105  
 106          $token = $this->parseAccessTokenResponse(json_encode($parsedResult));
 107          $this->storage->storeAccessToken($this->service(), $token);
 108  
 109          return $token;
 110      }
 111  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1