Reddit.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Reddit extends AbstractService
  11. {
  12. /**
  13. * Defined scopes
  14. *
  15. * @link http://www.reddit.com/dev/api/oauth
  16. */
  17. // User scopes
  18. const SCOPE_EDIT = 'edit';
  19. const SCOPE_HISTORY = 'history';
  20. const SCOPE_IDENTITY = 'identity';
  21. const SCOPE_MYSUBREDDITS = 'mysubreddits';
  22. const SCOPE_PRIVATEMESSAGES = 'privatemessages';
  23. const SCOPE_READ = 'read';
  24. const SCOPE_SAVE = 'save';
  25. const SCOPE_SUBMIT = 'submit';
  26. const SCOPE_SUBSCRIBE = 'subscribe';
  27. const SCOPE_VOTE = 'vote';
  28. // Mod Scopes
  29. const SCOPE_MODCONFIG = 'modconfig';
  30. const SCOPE_MODFLAIR = 'modflair';
  31. const SCOPE_MODLOG = 'modlog';
  32. const SCOPE_MODPOST = 'modpost';
  33. public function __construct(
  34. CredentialsInterface $credentials,
  35. ClientInterface $httpClient,
  36. TokenStorageInterface $storage,
  37. $scopes = array(),
  38. UriInterface $baseApiUri = null
  39. ) {
  40. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  41. if (null === $baseApiUri) {
  42. $this->baseApiUri = new Uri('https://oauth.reddit.com');
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getAuthorizationEndpoint()
  49. {
  50. return new Uri('https://ssl.reddit.com/api/v1/authorize');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getAccessTokenEndpoint()
  56. {
  57. return new Uri('https://ssl.reddit.com/api/v1/access_token');
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function getAuthorizationMethod()
  63. {
  64. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function parseAccessTokenResponse($responseBody)
  70. {
  71. $data = json_decode($responseBody, true);
  72. if (null === $data || !is_array($data)) {
  73. throw new TokenResponseException('Unable to parse response.');
  74. } elseif (isset($data['error'])) {
  75. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  76. }
  77. $token = new StdOAuth2Token();
  78. $token->setAccessToken($data['access_token']);
  79. $token->setLifeTime($data['expires_in']);
  80. if (isset($data['refresh_token'])) {
  81. $token->setRefreshToken($data['refresh_token']);
  82. unset($data['refresh_token']);
  83. }
  84. unset($data['access_token']);
  85. unset($data['expires_in']);
  86. $token->setExtraParams($data);
  87. return $token;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function getExtraOAuthHeaders()
  93. {
  94. // Reddit uses a Basic OAuth header
  95. return array('Authorization' => 'Basic ' .
  96. base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
  97. }
  98. }