Deezer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Deezer service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link http://developers.deezer.com/api/
  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. * Deezer service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link http://developers.deezer.com/api/
  23. */
  24. class Deezer extends AbstractService
  25. {
  26. /**
  27. * Defined scopes
  28. * http://developers.deezer.com/api/permissions
  29. */
  30. const SCOPE_BASIC_ACCESS = 'basic_access'; // Access users basic information
  31. const SCOPE_EMAIL = 'email'; // Get the user's email
  32. const SCOPE_OFFLINE_ACCESS = 'offline_access'; // Access user data any time
  33. const SCOPE_MANAGE_LIBRARY = 'manage_library'; // Manage users' library
  34. const SCOPE_MANAGE_COMMUNITY = 'manage_community'; // Manage users' friends
  35. const SCOPE_DELETE_LIBRARY = 'delete_library'; // Delete library items
  36. const SCOPE_LISTENING_HISTORY = 'listening_history'; // Access the user's listening history
  37. public function __construct(
  38. CredentialsInterface $credentials,
  39. ClientInterface $httpClient,
  40. TokenStorageInterface $storage,
  41. $scopes = array(),
  42. UriInterface $baseApiUri = null
  43. ) {
  44. parent::__construct(
  45. $credentials,
  46. $httpClient,
  47. $storage,
  48. $scopes,
  49. $baseApiUri,
  50. true
  51. );
  52. if (null === $baseApiUri) {
  53. $this->baseApiUri = new Uri('https://api.deezer.com/');
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getAuthorizationEndpoint()
  60. {
  61. return new Uri('https://connect.deezer.com/oauth/auth.php');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getAccessTokenEndpoint()
  67. {
  68. return new Uri('https://connect.deezer.com/oauth/access_token.php');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function getAuthorizationMethod()
  74. {
  75. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function parseAccessTokenResponse($responseBody)
  81. {
  82. parse_str($responseBody, $data);
  83. if (null === $data || !is_array($data) || empty($data)) {
  84. throw new TokenResponseException('Unable to parse response.');
  85. } elseif (isset($data['error'])) {
  86. throw new TokenResponseException(
  87. 'Error in retrieving token: "' . $data['error'] . '"'
  88. );
  89. } elseif (isset($data['error_reason'])) {
  90. throw new TokenResponseException(
  91. 'Error in retrieving token: "' . $data['error_reason'] . '"'
  92. );
  93. }
  94. $token = new StdOAuth2Token();
  95. $token->setAccessToken($data['access_token']);
  96. $token->setLifeTime($data['expires']);
  97. // I hope one day Deezer add a refresh token :)
  98. if (isset($data['refresh_token'])) {
  99. $token->setRefreshToken($data['refresh_token']);
  100. unset($data['refresh_token']);
  101. }
  102. unset($data['access_token']);
  103. unset($data['expires']);
  104. $token->setExtraParams($data);
  105. return $token;
  106. }
  107. }