Skip to content

Commit 1029cb4

Browse files
committed
refactor oauth for inclusion of helix
- adds MobileClient().prepare_token_uri() for generating app access tokens, adds client_secret requirement - adds MobileClient().prepare_revoke_uri() for revoking oauth tokens - adds helix oauth scopes - twitch.scopes -> twitch.oauth.<api version>.scopes - twitch.oauth.MobileClient() -> twitch.oauth.clients.MobileClient()
1 parent fd481e5 commit 1029cb4

8 files changed

Lines changed: 73 additions & 30 deletions

File tree

resources/lib/twitch/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
VERSION = '1.3.0'
44
CLIENT_ID = ''
5+
CLIENT_SECRET = ''
56
OAUTH_TOKEN = ''
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from twitch.oauth import v5 # V5 is deprecated and will be removed entirely on 2/14/18
4+
from twitch.oauth import helix
5+
from twitch.oauth import v5 as default
6+
from twitch.oauth import clients
7+
8+
__all__ = ['v5', 'default', 'helix', 'clients']
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# -*- encoding: utf-8 -*-
22

3-
from twitch import CLIENT_ID
4-
from twitch import scopes
3+
from twitch import CLIENT_ID, CLIENT_SECRET
4+
55
from six.moves.urllib_parse import urlsplit, urlencode
66

77

88
class MobileClient:
9-
_auth_base_url = 'https://api.twitch.tv/kraken/oauth2/authorize'
9+
_base_url = 'https://api.twitch.tv/kraken/oauth2/{0}'
1010

11-
def __init__(self, client_id=''):
11+
def __init__(self, client_id='', client_secret=''):
1212
self.client_id = client_id if client_id else CLIENT_ID
13+
self.client_secret = client_secret if client_secret else CLIENT_SECRET
1314

1415
def prepare_request_uri(self, redirect_uri='http://localhost:3000/', scope=list(), force_verify=False, state=''):
1516
params = {'response_type': 'token',
@@ -19,7 +20,22 @@ def prepare_request_uri(self, redirect_uri='http://localhost:3000/', scope=list(
1920
'force_verify': str(force_verify).lower(),
2021
'state': state}
2122
params = urlencode(params)
22-
url = '{base_uri}?{params}'.format(base_uri=self._auth_base_url, params=params)
23+
url = '{base_uri}?{params}'.format(base_uri=self._base_url.format('authorize'), params=params)
24+
return url
25+
26+
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
33+
34+
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)
2339
return url
2440

2541
@staticmethod
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from twitch.oauth.helix import scopes
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- encoding: utf-8 -*-
2+
# https://dev.twitch.tv/docs/authentication#scopes
3+
"""
4+
scopes
5+
6+
string constants
7+
"""
8+
9+
user_edit = 'user:edit' # Manage a user object.
10+
user_read_email = 'user:read:email' # Read authorized user's email address.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from twitch.oauth.v5 import scopes
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- encoding: utf-8 -*-
2+
# https://dev.twitch.tv/docs/authentication#scopes
3+
"""
4+
scopes
5+
6+
string constants
7+
"""
8+
9+
user_read = 'user_read' # Read nonpublic user information, like email address.
10+
user_blocks_edit = 'user_blocks_edit' # Turn on/off ignoring a user. Ignoring a user means you cannot see him type, receive messages from him, etc.
11+
user_blocks_read = 'user_blocks_read' # Read a user’s list of ignored users.
12+
user_follows_edit = 'user_follows_edit' # Manage a user’s followed channels.
13+
channel_read = 'channel_read' # Read nonpublic channel information, including email address and stream key.
14+
channel_editor = 'channel_editor' # Write channel metadata (game, status, etc).
15+
channel_commercial = 'channel_commercial' # Trigger commercials on channel.
16+
channel_stream = 'channel_stream' # Reset a channel’s stream key.
17+
channel_subscriptions = 'channel_subscriptions' # Read all subscribers to your channel.
18+
user_subscriptions = 'user_subscriptions' # Read a user’s subscriptions.
19+
channel_check_subscription = 'channel_check_subscription' # Read whether a user is subscribed to your channel.
20+
chat_login = 'chat_login' # Log into chat and send messages.
21+
channel_feed_read = 'channel_feed_read' # View a channel feed.
22+
channel_feed_edit = 'channel_feed_edit' # Add posts and reactions to a channel feed.
23+
collections_edit = 'collections_edit' # Manage a user's collections (of videos).
24+
communities_edit = 'communities_edit' # Manage a user's communities.
25+
communities_moderate = 'communities_moderate' # Manage community moderators.
26+
viewing_activity_read = 'viewing_activity_read' # Turn on Viewer Heartbeat Service ability to record user data.
27+
openid = 'openid' # Use OpenID Connect authentication.

resources/lib/twitch/scopes.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)