1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
6
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
7
|
|
|
use OAuth\Common\Http\Uri\Uri; |
8
|
|
|
|
9
|
|
|
class SoundCloud extends AbstractService |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
protected function init() |
15
|
|
|
{ |
16
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
17
|
|
|
$this->baseApiUri = new Uri('https://api.soundcloud.com/'); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function getAuthorizationEndpoint() |
25
|
|
|
{ |
26
|
|
|
return new Uri('https://soundcloud.com/connect'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function getAccessTokenEndpoint() |
33
|
|
|
{ |
34
|
|
|
return new Uri('https://api.soundcloud.com/oauth2/token'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
protected function parseAccessTokenResponse($responseBody) |
41
|
|
|
{ |
42
|
|
|
$data = json_decode($responseBody, true); |
43
|
|
|
|
44
|
|
|
if (null === $data || !is_array($data)) { |
45
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
46
|
|
|
} elseif (isset($data['error'])) { |
47
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$token = new StdOAuth2Token(); |
51
|
|
|
$token->setAccessToken($data['access_token']); |
52
|
|
|
|
53
|
|
|
if (isset($data['expires_in'])) { |
54
|
|
|
$token->setLifetime($data['expires_in']); |
55
|
|
|
unset($data['expires_in']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($data['refresh_token'])) { |
59
|
|
|
$token->setRefreshToken($data['refresh_token']); |
60
|
|
|
unset($data['refresh_token']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
unset($data['access_token']); |
64
|
|
|
|
65
|
|
|
$token->setExtraParams($data); |
66
|
|
|
|
67
|
|
|
return $token; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|