Skip to content

Commit 8d3afca

Browse files
authored
Merge pull request #38 from anxdpanic/dev
refactor MobileClient, add revoke_token, get_app_access_token queries
2 parents 5060e28 + 758292c commit 8d3afca

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

addon.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
[chg] refactor oauth for inclusion of helix
1313
[add] helix api
1414
[add] add usher.live_request and usher.video_request
15+
[add] MobileClient().revoke_token and MobileClient().get_app_access_token queries
1516
</news>
1617
<assets>
1718
<icon>icon.png</icon>

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- twitch.oauth.MobileClient() -> twitch.oauth.clients.MobileClient()
88
[add] helix api
99
[add] add usher.live_request and usher.video_request
10+
[add] MobileClient().revoke_token and MobileClient().get_app_access_token queries
1011

1112
1.1.0
1213
*** Twitch API V5 is deprecated and will be removed entirely on 2/14/18

resources/lib/twitch/oauth/clients.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
# -*- encoding: utf-8 -*-
22

3-
from twitch import CLIENT_ID, CLIENT_SECRET
3+
from twitch import CLIENT_ID, CLIENT_SECRET, methods
4+
from twitch.queries import OAuthQuery as Qry
5+
from twitch.queries import query
46

57
from six.moves.urllib_parse import urlsplit, urlencode
68

79

810
class MobileClient:
9-
_base_url = 'https://api.twitch.tv/kraken/oauth2/{0}'
10-
1111
def __init__(self, client_id='', client_secret=''):
1212
self.client_id = client_id if client_id else CLIENT_ID
1313
self.client_secret = client_secret if client_secret else CLIENT_SECRET
1414

1515
def prepare_request_uri(self, redirect_uri='http://localhost:3000/', scope=list(), force_verify=False, state=''):
16-
params = {'response_type': 'token',
17-
'client_id': self.client_id,
18-
'redirect_uri': redirect_uri,
19-
'scope': ' '.join(scope),
20-
'force_verify': str(force_verify).lower(),
21-
'state': state}
22-
params = urlencode(params)
23-
url = '{base_uri}?{params}'.format(base_uri=self._base_url.format('authorize'), params=params)
24-
return url
16+
q = Qry('authorize')
17+
q.add_param('response_type', 'token')
18+
q.add_param('client_id', self.client_id)
19+
q.add_param('redirect_uri', redirect_uri)
20+
q.add_param('scope', ' '.join(scope))
21+
q.add_param('force_verify', str(force_verify).lower())
22+
q.add_param('state', state)
23+
return '?'.join([q.url, urlencode(q.params)])
2524

2625
def prepare_token_uri(self, scope=list()):
27-
params = {'client_id': self.client_id,
28-
'client_secret': self.client_secret,
29-
'scope': ' '.join(scope)}
30-
params = urlencode(params)
31-
url = '{base_uri}?{params}'.format(base_uri=self._base_url.format('token'), params=params)
32-
return url
26+
q = Qry('token')
27+
q.add_param('client_id', self.client_id)
28+
q.add_param('client_secret', self.client_secret)
29+
q.add_param('grant_type', 'client_credentials')
30+
q.add_param('scope', ' '.join(scope))
31+
return '?'.join([q.url, urlencode(q.params)])
3332

3433
def prepare_revoke_uri(self, token):
35-
params = {'client_id': self.client_id,
36-
'token': token}
37-
params = urlencode(params)
38-
url = '{base_uri}?{params}'.format(base_uri=self._base_url.format('revoke'), params=params)
39-
return url
34+
q = Qry('revoke')
35+
q.add_param('client_id', self.client_id)
36+
q.add_param('token', token)
37+
return '?'.join([q.url, urlencode(q.params)])
38+
39+
@query
40+
def revoke_token(self, token):
41+
q = Qry('revoke', method=methods.POST)
42+
q.add_param('client_id', self.client_id)
43+
q.add_param('token', token)
44+
return q
45+
46+
@query
47+
def get_app_access_token(self, scope=list()):
48+
q = Qry('token', method=methods.POST)
49+
q.add_param('client_id', self.client_id)
50+
q.add_param('client_secret', self.client_secret)
51+
q.add_param('grant_type', 'client_credentials')
52+
q.add_param('scope', ' '.join(scope))
53+
return q
4054

4155
@staticmethod
4256
def parse_implicit_response(url):

resources/lib/twitch/queries.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
_usher_baseurl = 'https://usher.ttvnw.net/'
1515
_clips_baseurl = 'https://clips.twitch.tv/'
1616
_uploads_baseurl = 'https://uploads.twitch.tv/'
17+
_oauth_baseurl = 'https://api.twitch.tv/kraken/oauth2/'
1718

1819
_v5_headers = {'ACCEPT': 'application/vnd.twitchtv.v5+json'}
1920

@@ -161,6 +162,12 @@ def __init__(self, path, headers={}, data={}, method=methods.GET):
161162
self.add_path(path)
162163

163164

165+
class OAuthQuery(JsonQuery):
166+
def __init__(self, path, headers={}, data={}, method=methods.GET):
167+
super(JsonQuery, self).__init__(_oauth_baseurl, headers, data, method)
168+
self.add_path(path)
169+
170+
164171
class ClipsQuery(DownloadQuery):
165172
def __init__(self, path, headers={}, data={}, method=methods.GET):
166173
super(ClipsQuery, self).__init__(_clips_baseurl, headers, data, method)

0 commit comments

Comments
 (0)