|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kerox\OAuth2\Client\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Kerox\OAuth2\Client\Provider\Exception\SpotifyIdentityProviderException; |
|
8
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
9
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
10
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
|
11
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
12
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Spotify extends AbstractProvider |
|
16
|
|
|
{ |
|
17
|
|
|
use BearerAuthorizationTrait; |
|
18
|
|
|
|
|
19
|
|
|
public const BASE_SPOTIFY_URL = 'https://accounts.spotify.com/'; |
|
20
|
|
|
public const RESPONSE_TYPE = 'code'; |
|
21
|
|
|
|
|
22
|
|
|
// Available scopes. |
|
23
|
|
|
|
|
24
|
|
|
// Images |
|
25
|
|
|
public const SCOPE_UGC_IMAGE_UPLOAD = 'ugc-image-upload'; |
|
26
|
|
|
|
|
27
|
|
|
// Spotify Connect |
|
28
|
|
|
public const SCOPE_USER_MODIFY_PLAYBACK_STATE = 'user-modify-playback-state'; |
|
29
|
|
|
public const SCOPE_USER_READ_PLAYBACK_STATE = 'user-read-playback-state'; |
|
30
|
|
|
public const SCOPE_USER_READ_CURRENTLY_PLAYING = 'user-read-currently-playing'; |
|
31
|
|
|
|
|
32
|
|
|
// Listening History |
|
33
|
|
|
public const SCOPE_USER_TOP_READ = 'user-top-read'; |
|
34
|
|
|
public const SCOPE_USER_READ_RECENTLY_PLAYED = 'user-read-recently-played'; |
|
35
|
|
|
|
|
36
|
|
|
// Library |
|
37
|
|
|
public const SCOPE_USER_LIBRARY_MODIFY = 'user-library-modify'; |
|
38
|
|
|
public const SCOPE_USER_LIBRARY_READ = 'user-library-read'; |
|
39
|
|
|
|
|
40
|
|
|
// Follow |
|
41
|
|
|
public const SCOPE_USER_FOLLOW_MODIFY = 'user-follow-modify'; |
|
42
|
|
|
public const SCOPE_USER_FOLLOW_READ = 'user-follow-read'; |
|
43
|
|
|
|
|
44
|
|
|
// Playlist |
|
45
|
|
|
public const SCOPE_PLAYLIST_READ_PRIVATE = 'playlist-read-private'; |
|
46
|
|
|
public const SCOPE_PLAYLIST_MODIFY_PUBLIC = 'playlist-modify-public'; |
|
47
|
|
|
public const SCOPE_PLAYLIST_MODIFY_PRIVATE = 'playlist-modify-private'; |
|
48
|
|
|
public const SCOPE_PLAYLIST_READ_COLLABORATIVE = 'playlist-read-collaborative'; |
|
49
|
|
|
|
|
50
|
|
|
// User |
|
51
|
|
|
public const SCOPE_USER_READ_PRIVATE = 'user-read-private'; |
|
52
|
|
|
public const SCOPE_USER_READ_EMAIL = 'user-read-email'; |
|
53
|
|
|
|
|
54
|
|
|
// Playback |
|
55
|
|
|
public const SCOPE_APP_REMOTE_CONTROL = 'app-remote-control'; |
|
56
|
|
|
public const SCOPE_STREAMING = 'streaming'; |
|
57
|
|
|
|
|
58
|
|
|
public function __construct(array $options = [], array $collaborators = []) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!isset($options['responseType']) || $options['responseType'] !== self::RESPONSE_TYPE) { |
|
61
|
8 |
|
$options['responseType'] = self::RESPONSE_TYPE; |
|
62
|
|
|
} |
|
63
|
8 |
|
|
|
64
|
1 |
|
parent::__construct($options, $collaborators); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
8 |
|
/** |
|
68
|
8 |
|
* Returns the base URL for authorizing a client. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getBaseAuthorizationUrl(): string |
|
71
|
|
|
{ |
|
72
|
|
|
return self::BASE_SPOTIFY_URL . 'authorize'; |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
/** |
|
76
|
|
|
* Returns the base URL for requesting an access token. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getBaseAccessTokenUrl(array $params): string |
|
79
|
|
|
{ |
|
80
|
|
|
return self::BASE_SPOTIFY_URL . 'api/token'; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
/** |
|
84
|
|
|
* Returns the URL for requesting the resource owner's details. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token): string |
|
87
|
|
|
{ |
|
88
|
|
|
return 'https://api.spotify.com/v1/me'; |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
/** |
|
92
|
|
|
* Returns the default scopes used by this provider. |
|
93
|
|
|
* |
|
94
|
|
|
* This should only be the scopes that are required to request the details |
|
95
|
|
|
* of the resource owner, rather than all the available scopes. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getDefaultScopes(): array |
|
98
|
|
|
{ |
|
99
|
|
|
return []; |
|
100
|
2 |
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
/** |
|
103
|
|
|
* Checks a provider response for errors. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array|string $data Parsed response data |
|
106
|
|
|
* |
|
107
|
|
|
* @throws IdentityProviderException |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function checkResponse(ResponseInterface $response, $data): void |
|
110
|
|
|
{ |
|
111
|
|
|
if ($response->getStatusCode() >= 400) { |
|
112
|
3 |
|
$error = $data['error_description'] ?? $data['error'] ?? $response->getReasonPhrase(); |
|
113
|
|
|
$statusCode = $response->getStatusCode(); |
|
114
|
3 |
|
|
|
115
|
2 |
|
if (\is_array($data['error'])) { |
|
116
|
2 |
|
$error = $data['error']['message']; |
|
117
|
|
|
$statusCode = $data['error']['status']; |
|
118
|
2 |
|
} |
|
119
|
1 |
|
|
|
120
|
1 |
|
throw new SpotifyIdentityProviderException($error, $statusCode, $response); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
2 |
|
|
|
124
|
|
|
/** |
|
125
|
1 |
|
* Generates a resource owner object from a successful resource owner |
|
126
|
|
|
* details request. |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function createResourceOwner(array $response, AccessToken $token): ResourceOwnerInterface |
|
129
|
|
|
{ |
|
130
|
|
|
return new SpotifyResourceOwner($response); |
|
131
|
1 |
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: