Bitly.php 3.2 KB

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