Skip to content

Commit 758292c

Browse files
committed
refactor MobileClient, add revoke_token, get_app_access_token queries
1 parent d690889 commit 758292c

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-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):

0 commit comments

Comments
 (0)