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