ScoopIt.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ScoopIt extends AbstractService
  12. {
  13. public function __construct(
  14. CredentialsInterface $credentials,
  15. ClientInterface $httpClient,
  16. TokenStorageInterface $storage,
  17. SignatureInterface $signature,
  18. UriInterface $baseApiUri = null
  19. ) {
  20. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  21. if (null === $baseApiUri) {
  22. $this->baseApiUri = new Uri('https://www.scoop.it/api/1/');
  23. }
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function getRequestTokenEndpoint()
  29. {
  30. return new Uri('https://www.scoop.it/oauth/request');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getAuthorizationEndpoint()
  36. {
  37. return new Uri('https://www.scoop.it/oauth/authorize');
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getAccessTokenEndpoint()
  43. {
  44. return new Uri('https://www.scoop.it/oauth/access');
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function parseRequestTokenResponse($responseBody)
  50. {
  51. parse_str($responseBody, $data);
  52. if (null === $data || !is_array($data)) {
  53. throw new TokenResponseException('Unable to parse response.');
  54. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
  55. throw new TokenResponseException('Error in retrieving token.');
  56. }
  57. return $this->parseAccessTokenResponse($responseBody);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function parseAccessTokenResponse($responseBody)
  63. {
  64. parse_str($responseBody, $data);
  65. if (null === $data || !is_array($data)) {
  66. throw new TokenResponseException('Unable to parse response.');
  67. } elseif (isset($data['error'])) {
  68. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  69. }
  70. $token = new StdOAuth1Token();
  71. $token->setRequestToken($data['oauth_token']);
  72. $token->setRequestTokenSecret($data['oauth_token_secret']);
  73. $token->setAccessToken($data['oauth_token']);
  74. $token->setAccessTokenSecret($data['oauth_token_secret']);
  75. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  76. unset($data['oauth_token'], $data['oauth_token_secret']);
  77. $token->setExtraParams($data);
  78. return $token;
  79. }
  80. }