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
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
9
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
10
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
11
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
12
|
|
|
|
13
|
|
|
class Twitch extends AbstractService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Scopes defined at: |
17
|
|
|
* https://github.com/justintv/Twitch-API/blob/master/authentication.md#scopes |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Read access to non-public user information, such as email address. |
22
|
|
|
*/ |
23
|
|
|
const SCOPE_USER_READ = 'user_read'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Ability to ignore or unignore on behalf of a user. |
27
|
|
|
*/ |
28
|
|
|
const SCOPE_BLOCKS_EDIT = 'user_blocks_edit'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Read access to a user's list of ignored users. |
32
|
|
|
*/ |
33
|
|
|
const SCOPE_BLOCKS_READ = 'user_blocks_read'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Access to manage a user's followed channels. |
37
|
|
|
*/ |
38
|
|
|
const SCOPE_CHANNEL_READ = 'channel_read'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Write access to channel metadata (game, status, etc). |
42
|
|
|
*/ |
43
|
|
|
const SCOPE_CHANNEL_EDITOR = 'channel_editor'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Access to trigger commercials on channel. |
47
|
|
|
*/ |
48
|
|
|
const SCOPE_CHANNEL_COMMERCIAL = 'channel_commercial'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Ability to reset a channel's stream key. |
52
|
|
|
*/ |
53
|
|
|
const SCOPE_CHANNEL_STREAM = 'channnel_stream'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Read access to all subscribers to your channel. |
57
|
|
|
*/ |
58
|
|
|
const SCOPE_CHANNEL_SUBSCRIPTIONS = 'channel_subscriptions'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Read access to subscriptions of a user. |
62
|
|
|
*/ |
63
|
|
|
const SCOPE_USER_SUBSCRIPTIONS = 'user_subscriptions'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Read access to check if a user is subscribed to your channel. |
67
|
|
|
*/ |
68
|
|
|
const SCOPE_CHANNEL_CHECK_SUBSCRIPTION = 'channel_check_subscription'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Ability to log into chat and send messages. |
72
|
|
|
*/ |
73
|
|
|
const SCOPE_CHAT_LOGIN = 'chat_login'; |
74
|
|
|
|
75
|
|
|
public function __construct( |
76
|
|
|
CredentialsInterface $credentials, |
77
|
|
|
ClientInterface $httpClient, |
78
|
|
|
TokenStorageInterface $storage, |
79
|
|
|
$scopes = array(), |
80
|
|
|
UriInterface $baseApiUri = null, |
81
|
|
|
$stateParameterInAutUrl = true, |
82
|
|
|
$apiVersion = "v3" |
83
|
|
|
) { |
84
|
|
|
parent::__construct( |
85
|
|
|
$credentials, |
86
|
|
|
$httpClient, |
87
|
|
|
$storage, |
88
|
|
|
$scopes, |
89
|
|
|
$baseApiUri, |
90
|
|
|
$stateParameterInAutUrl, |
91
|
|
|
$apiVersion |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if (null === $baseApiUri) { |
95
|
|
|
$this->baseApiUri = new Uri('https://api.twitch.tv/kraken/'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function getAuthorizationEndpoint() |
103
|
|
|
{ |
104
|
|
|
return new Uri('https://api.twitch.tv/kraken/oauth2/authorize'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getAccessTokenEndpoint() |
111
|
|
|
{ |
112
|
|
|
return new Uri('https://api.twitch.tv/kraken/oauth2/token'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
protected function parseAccessTokenResponse($responseBody) |
119
|
|
|
{ |
120
|
|
|
$data = json_decode($responseBody, true); |
121
|
|
|
|
122
|
|
|
if (null === $data || !is_array($data)) { |
123
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
124
|
|
|
} elseif (isset($data['error'])) { |
125
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$token = new StdOAuth2Token(); |
129
|
|
|
$token->setAccessToken($data['access_token']); |
130
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
131
|
|
|
unset($data['access_token']); |
132
|
|
|
|
133
|
|
|
$token->setExtraParams($data); |
134
|
|
|
|
135
|
|
|
return $token; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
protected function getExtraApiHeaders() |
142
|
|
|
{ |
143
|
|
|
return array( |
144
|
|
|
'Accept' => 'application/vnd.twitchtv.'.$this->apiVersion.'+json', |
145
|
|
|
'Client-ID' => $this->credentials->getConsumerId() |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|