DeviantArt.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\Common\Exception\Exception;
  4. use OAuth\OAuth2\Token\StdOAuth2Token;
  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\Client\ClientInterface;
  9. use OAuth\Common\Storage\TokenStorageInterface;
  10. use OAuth\Common\Http\Uri\UriInterface;
  11. class DeviantArt extends AbstractService
  12. {
  13. /**
  14. * DeviantArt www url - used to build dialog urls
  15. */
  16. const WWW_URL = 'https://www.deviantart.com/';
  17. /**
  18. * Defined scopes
  19. *
  20. * If you don't think this is scary you should not be allowed on the web at all
  21. *
  22. * @link https://www.deviantart.com/developers/authentication
  23. * @link https://www.deviantart.com/developers/http/v1/20150217
  24. */
  25. const SCOPE_FEED = 'feed';
  26. const SCOPE_BROWSE = 'browse';
  27. const SCOPE_COMMENT = 'comment.post';
  28. const SCOPE_STASH = 'stash';
  29. const SCOPE_USER = 'user';
  30. const SCOPE_USERMANAGE = 'user.manage';
  31. public function __construct(
  32. CredentialsInterface $credentials,
  33. ClientInterface $httpClient,
  34. TokenStorageInterface $storage,
  35. $scopes = array(),
  36. UriInterface $baseApiUri = null
  37. ) {
  38. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  39. if (null === $baseApiUri) {
  40. $this->baseApiUri = new Uri('https://www.deviantart.com/api/v1/oauth2/');
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getAuthorizationEndpoint()
  47. {
  48. return new Uri('https://www.deviantart.com/oauth2/authorize');
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getAccessTokenEndpoint()
  54. {
  55. return new Uri('https://www.deviantart.com/oauth2/token');
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function parseAccessTokenResponse($responseBody)
  61. {
  62. $data = json_decode($responseBody, true);
  63. if (null === $data || !is_array($data)) {
  64. throw new TokenResponseException('Unable to parse response.');
  65. } elseif (isset($data['error'])) {
  66. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  67. }
  68. $token = new StdOAuth2Token();
  69. $token->setAccessToken($data['access_token']);
  70. if (isset($data['expires_in'])) {
  71. $token->setLifeTime($data['expires_in']);
  72. }
  73. if (isset($data['refresh_token'])) {
  74. $token->setRefreshToken($data['refresh_token']);
  75. unset($data['refresh_token']);
  76. }
  77. unset($data['access_token']);
  78. unset($data['expires_in']);
  79. $token->setExtraParams($data);
  80. return $token;
  81. }
  82. }