[ Index ]

PHP Cross Reference of phpBB-3.1.12-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)
  81      {
  82          $bodyParams = array(
  83              'code'          => $code,
  84              'client_id'     => $this->credentials->getConsumerId(),
  85              'client_secret' => $this->credentials->getConsumerSecret(),
  86              'redirect_uri'  => $this->credentials->getCallbackUrl(),
  87              'grant_type'    => 'authorization_code',
  88          );
  89  
  90          $responseBody = $this->httpClient->retrieveResponse(
  91              $this->getAccessTokenEndpoint(),
  92              $bodyParams,
  93              $this->getExtraOAuthHeaders()
  94          );
  95  
  96          // we can scream what we want that we want bitly to return a json encoded string (format=json), but the
  97          // WOAH WATCH YOUR LANGUAGE ;) service doesn't seem to like screaming, hence we need to manually
  98          // parse the result
  99          $parsedResult = array();
 100          parse_str($responseBody, $parsedResult);
 101  
 102          $token = $this->parseAccessTokenResponse(json_encode($parsedResult));
 103          $this->storage->storeAccessToken($this->service(), $token);
 104  
 105          return $token;
 106      }
 107  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1