FiveHundredPx.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * 500px service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://developers.500px.com/
  8. */
  9. namespace OAuth\OAuth1\Service;
  10. use OAuth\OAuth1\Signature\SignatureInterface;
  11. use OAuth\OAuth1\Token\StdOAuth1Token;
  12. use OAuth\Common\Http\Exception\TokenResponseException;
  13. use OAuth\Common\Http\Uri\Uri;
  14. use OAuth\Common\Consumer\CredentialsInterface;
  15. use OAuth\Common\Http\Uri\UriInterface;
  16. use OAuth\Common\Storage\TokenStorageInterface;
  17. use OAuth\Common\Http\Client\ClientInterface;
  18. /**
  19. * 500px service.
  20. *
  21. * @author Pedro Amorim <contact@pamorim.fr>
  22. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  23. * @link https://developers.500px.com/
  24. */
  25. class FiveHundredPx extends AbstractService
  26. {
  27. public function __construct(
  28. CredentialsInterface $credentials,
  29. ClientInterface $httpClient,
  30. TokenStorageInterface $storage,
  31. SignatureInterface $signature,
  32. UriInterface $baseApiUri = null
  33. ) {
  34. parent::__construct(
  35. $credentials,
  36. $httpClient,
  37. $storage,
  38. $signature,
  39. $baseApiUri
  40. );
  41. if (null === $baseApiUri) {
  42. $this->baseApiUri = new Uri('https://api.500px.com/v1/');
  43. }
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function getRequestTokenEndpoint()
  49. {
  50. return new Uri('https://api.500px.com/v1/oauth/request_token');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getAuthorizationEndpoint()
  56. {
  57. return new Uri('https://api.500px.com/v1/oauth/authorize');
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getAccessTokenEndpoint()
  63. {
  64. return new Uri('https://api.500px.com/v1/oauth/access_token');
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function parseRequestTokenResponse($responseBody)
  70. {
  71. parse_str($responseBody, $data);
  72. if (null === $data || !is_array($data)) {
  73. throw new TokenResponseException('Unable to parse response.');
  74. } elseif (!isset($data['oauth_callback_confirmed'])
  75. || $data['oauth_callback_confirmed'] !== 'true'
  76. ) {
  77. throw new TokenResponseException('Error in retrieving token.');
  78. }
  79. return $this->parseAccessTokenResponse($responseBody);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function parseAccessTokenResponse($responseBody)
  85. {
  86. parse_str($responseBody, $data);
  87. if (null === $data || !is_array($data)) {
  88. throw new TokenResponseException('Unable to parse response.');
  89. } elseif (isset($data['error'])) {
  90. throw new TokenResponseException(
  91. 'Error in retrieving token: "' . $data['error'] . '"'
  92. );
  93. }
  94. $token = new StdOAuth1Token();
  95. $token->setRequestToken($data['oauth_token']);
  96. $token->setRequestTokenSecret($data['oauth_token_secret']);
  97. $token->setAccessToken($data['oauth_token']);
  98. $token->setAccessTokenSecret($data['oauth_token_secret']);
  99. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  100. unset($data['oauth_token'], $data['oauth_token_secret']);
  101. $token->setExtraParams($data);
  102. return $token;
  103. }
  104. }