Spotify.php 3.0 KB

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