Delicious.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Delicious service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
  8. */
  9. namespace OAuth\OAuth2\Service;
  10. use OAuth\OAuth2\Token\StdOAuth2Token;
  11. use OAuth\Common\Http\Exception\TokenResponseException;
  12. use OAuth\Common\Http\Uri\Uri;
  13. use OAuth\Common\Consumer\CredentialsInterface;
  14. use OAuth\Common\Http\Client\ClientInterface;
  15. use OAuth\Common\Storage\TokenStorageInterface;
  16. use OAuth\Common\Http\Uri\UriInterface;
  17. /**
  18. * Delicious service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link https://github.com/SciDevs/delicious-api/blob/master/api/oauth.md
  23. */
  24. class Delicious extends AbstractService
  25. {
  26. public function __construct(
  27. CredentialsInterface $credentials,
  28. ClientInterface $httpClient,
  29. TokenStorageInterface $storage,
  30. $scopes = array(),
  31. UriInterface $baseApiUri = null
  32. ) {
  33. parent::__construct(
  34. $credentials,
  35. $httpClient,
  36. $storage,
  37. $scopes,
  38. $baseApiUri,
  39. true
  40. );
  41. if (null === $baseApiUri) {
  42. $this->baseApiUri = new Uri('https://api.del.icio.us/v1/');
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getAuthorizationEndpoint()
  49. {
  50. return new Uri('https://delicious.com/auth/authorize');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getAccessTokenEndpoint()
  56. {
  57. return new Uri('https://avosapi.delicious.com/api/v1/oauth/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(
  76. 'Error in retrieving token: "' . $data['error'] . '"'
  77. );
  78. }
  79. $token = new StdOAuth2Token();
  80. $token->setAccessToken($data['access_token']);
  81. if (isset($data['expires_in'])) {
  82. $token->setLifetime($data['expires_in']);
  83. unset($data['expires_in']);
  84. }
  85. if (isset($data['refresh_token'])) {
  86. $token->setRefreshToken($data['refresh_token']);
  87. unset($data['refresh_token']);
  88. }
  89. unset($data['access_token']);
  90. $token->setExtraParams($data);
  91. return $token;
  92. }
  93. // Special, delicious didn't respect the oauth2 RFC and need a grant_type='code'
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function requestAccessToken($code, $state = null)
  98. {
  99. if (null !== $state) {
  100. $this->validateAuthorizationState($state);
  101. }
  102. $bodyParams = array(
  103. 'code' => $code,
  104. 'client_id' => $this->credentials->getConsumerId(),
  105. 'client_secret' => $this->credentials->getConsumerSecret(),
  106. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  107. 'grant_type' => 'code',
  108. );
  109. $responseBody = $this->httpClient->retrieveResponse(
  110. $this->getAccessTokenEndpoint(),
  111. $bodyParams,
  112. $this->getExtraOAuthHeaders()
  113. );
  114. $token = $this->parseAccessTokenResponse($responseBody);
  115. $this->storage->storeAccessToken($this->service(), $token);
  116. return $token;
  117. }
  118. }