Spotify.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Spotify extends AbstractService
  11. {
  12. /**
  13. * Scopes
  14. *
  15. * @var string
  16. */
  17. const SCOPE_PLAYLIST_MODIFY_PUBLIC = 'playlist-modify-public';
  18. const SCOPE_PLAYLIST_MODIFY_PRIVATE = 'playlist-modify-private';
  19. const SCOPE_PLAYLIST_READ_PRIVATE = 'playlist-read-private';
  20. const SCOPE_PLAYLIST_READ_COLABORATIVE = 'playlist-read-collaborative';
  21. const SCOPE_STREAMING = 'streaming';
  22. const SCOPE_USER_LIBRARY_MODIFY = 'user-library-modify';
  23. const SCOPE_USER_LIBRARY_READ = 'user-library-read';
  24. const SCOPE_USER_READ_PRIVATE = 'user-read-private';
  25. const SCOPE_USER_READ_EMAIL = 'user-read-email';
  26. const SCOPE_USER_READ_BIRTHDAY = 'user-read-birthdate';
  27. const SCOPE_USER_READ_FOLLOW = 'user-follow-read';
  28. public function __construct(
  29. CredentialsInterface $credentials,
  30. ClientInterface $httpClient,
  31. TokenStorageInterface $storage,
  32. $scopes = array(),
  33. UriInterface $baseApiUri = null
  34. ) {
  35. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  36. if (null === $baseApiUri) {
  37. $this->baseApiUri = new Uri('https://api.spotify.com/v1/');
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getAuthorizationEndpoint()
  44. {
  45. return new Uri('https://accounts.spotify.com/authorize');
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getAccessTokenEndpoint()
  51. {
  52. return new Uri('https://accounts.spotify.com/api/token');
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function getAuthorizationMethod()
  58. {
  59. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function parseAccessTokenResponse($responseBody)
  65. {
  66. $data = json_decode($responseBody, true);
  67. if (null === $data || !is_array($data)) {
  68. throw new TokenResponseException('Unable to parse response.');
  69. } elseif (isset($data['error'])) {
  70. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  71. }
  72. $token = new StdOAuth2Token();
  73. $token->setAccessToken($data['access_token']);
  74. if (isset($data['expires_in'])) {
  75. $token->setLifetime($data['expires_in']);
  76. unset($data['expires_in']);
  77. }
  78. if (isset($data['refresh_token'])) {
  79. $token->setRefreshToken($data['refresh_token']);
  80. unset($data['refresh_token']);
  81. }
  82. unset($data['access_token']);
  83. $token->setExtraParams($data);
  84. return $token;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function getExtraOAuthHeaders()
  90. {
  91. return array('Authorization' => 'Basic ' .
  92. base64_encode($this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()));
  93. }
  94. }