Etsy.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace OAuth\OAuth1\Service;
  3. use OAuth\OAuth1\Signature\SignatureInterface;
  4. use OAuth\OAuth1\Token\StdOAuth1Token;
  5. use OAuth\Common\Http\Exception\TokenResponseException;
  6. use OAuth\Common\Http\Uri\Uri;
  7. use OAuth\Common\Consumer\CredentialsInterface;
  8. use OAuth\Common\Http\Uri\UriInterface;
  9. use OAuth\Common\Storage\TokenStorageInterface;
  10. use OAuth\Common\Http\Client\ClientInterface;
  11. class Etsy extends AbstractService
  12. {
  13. protected $scopes = array();
  14. public function __construct(
  15. CredentialsInterface $credentials,
  16. ClientInterface $httpClient,
  17. TokenStorageInterface $storage,
  18. SignatureInterface $signature,
  19. UriInterface $baseApiUri = null
  20. ) {
  21. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  22. if (null === $baseApiUri) {
  23. $this->baseApiUri = new Uri('https://openapi.etsy.com/v2/');
  24. }
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getRequestTokenEndpoint()
  30. {
  31. $uri = new Uri($this->baseApiUri . 'oauth/request_token');
  32. $scopes = $this->getScopes();
  33. if (count($scopes)) {
  34. $uri->setQuery('scope=' . implode('%20', $scopes));
  35. }
  36. return $uri;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getAuthorizationEndpoint()
  42. {
  43. return new Uri($this->baseApiUri);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getAccessTokenEndpoint()
  49. {
  50. return new Uri($this->baseApiUri . 'oauth/access_token');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function parseRequestTokenResponse($responseBody)
  56. {
  57. parse_str($responseBody, $data);
  58. if (null === $data || !is_array($data)) {
  59. throw new TokenResponseException('Unable to parse response.');
  60. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
  61. throw new TokenResponseException('Error in retrieving token.');
  62. }
  63. return $this->parseAccessTokenResponse($responseBody);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function parseAccessTokenResponse($responseBody)
  69. {
  70. parse_str($responseBody, $data);
  71. if (null === $data || !is_array($data)) {
  72. throw new TokenResponseException('Unable to parse response.');
  73. } elseif (isset($data['error'])) {
  74. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  75. }
  76. $token = new StdOAuth1Token();
  77. $token->setRequestToken($data['oauth_token']);
  78. $token->setRequestTokenSecret($data['oauth_token_secret']);
  79. $token->setAccessToken($data['oauth_token']);
  80. $token->setAccessTokenSecret($data['oauth_token_secret']);
  81. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  82. unset($data['oauth_token'], $data['oauth_token_secret']);
  83. $token->setExtraParams($data);
  84. return $token;
  85. }
  86. /**
  87. * Set the scopes for permissions
  88. * @see https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes
  89. * @param array $scopes
  90. *
  91. * @return $this
  92. */
  93. public function setScopes(array $scopes)
  94. {
  95. if (!is_array($scopes)) {
  96. $scopes = array();
  97. }
  98. $this->scopes = $scopes;
  99. return $this;
  100. }
  101. /**
  102. * Return the defined scopes
  103. * @return array
  104. */
  105. public function getScopes()
  106. {
  107. return $this->scopes;
  108. }
  109. }