From b82de9e8795221bb29c583224ef93605f772bd3f Mon Sep 17 00:00:00 2001 From: Christof Damian Date: Tue, 23 Mar 2021 09:43:29 +0100 Subject: [PATCH] Move token from the query strings to the session Slack stopped allowing tokens in query strings. See https://api.slack.com/changelog/2020-11-no-more-tokens-in-querystrings-for-newly-created-apps This moves the token handling into the request sessions object, which also cleans up the code a bit. Fixes #209 --- slacker.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/slacker.py b/slacker.py index 420307c..1f94437 100755 --- a/slacker.py +++ b/slacker.py @@ -22,12 +22,13 @@ def __init__(self, slack_name, token, init=True): assert self.token, "Token should not be blank" self.url = self.api_url() self.session = requests.Session() + self.session.headers.update({"Authorization": "Bearer " + token}) if init: self.get_users() self.get_channels() def get_emojis(self): - url = self.url + "emoji.list?token={}".format(self.token) + url = self.url + "emoji.list" return self.get_with_retry_to_json(url) def get_users(self): @@ -82,7 +83,7 @@ def get_messages_in_time_range(self, oldest, cid, latest=None): messages = [] done = False while not done: - murl = self.url + "conversations.history?oldest={}&token={}&channel={}".format(oldest, self.token, cid) + murl = self.url + "conversations.history?oldest={}&channel={}".format(oldest, cid) if latest: murl += "&latest={}".format(latest) else: @@ -183,8 +184,8 @@ def get_channel_members_ids(self, channel_name): if not member_count: return members # should be an empty set - url_template = self.url + "conversations.members?token={}&channel={}" - url = url_template.format(self.token, cid) + url_template = self.url + "conversations.members?channel={}" + url = url_template.format(cid) while True: ret = self.get_with_retry_to_json(url) @@ -198,8 +199,8 @@ def get_channel_members_ids(self, channel_name): # once through the loop once, update the url to call to include the cursor if ret['response_metadata']['next_cursor']: - url_template = self.url + "conversations.members?token={}&channel={}&cursor={}" - url = url_template.format(self.token, cid, ret['response_metadata']['next_cursor']) + url_template = self.url + "conversations.members?channel={}&cursor={}" + url = url_template.format(cid, ret['response_metadata']['next_cursor']) # no more members to iterate over else: break @@ -231,10 +232,10 @@ def get_channel_info(self, channel_name): returns JSON with channel information. Adds 'age' in seconds to JSON """ # ensure include_num_members is available for get_channel_member_count() - url_template = self.url + "conversations.info?token={}&channel={}&include_num_members=true" + url_template = self.url + "conversations.info?channel={}&include_num_members=true" cid = self.get_channelid(channel_name) now = int(time.time()) - url = url_template.format(self.token, cid) + url = url_template.format(cid) ret = self.get_with_retry_to_json(url) if ret['ok'] is not True: m = "Attempted get_channel_info() for {}, but return was {}" @@ -259,8 +260,8 @@ def get_all_channel_objects(self, exclude_archived=True): else: exclude_archived = 0 - url_template = self.url + "conversations.list?exclude_archived={}&token={}" - url = url_template.format(exclude_archived, self.token) + url_template = self.url + "conversations.list?exclude_archived={}" + url = url_template.format(exclude_archived) while True: ret = self.get_with_retry_to_json(url) @@ -274,8 +275,8 @@ def get_all_channel_objects(self, exclude_archived=True): # after going through the loop once, update the url to call to # include the pagination cursor if ret['response_metadata']['next_cursor']: - url_template = self.url + "conversations.list?exclude_archived={}&token={}&cursor={}" - url = url_template.format(exclude_archived, self.token, ret['response_metadata']['next_cursor']) + url_template = self.url + "conversations.list?exclude_archived={}&cursor={}" + url = url_template.format(exclude_archived, ret['response_metadata']['next_cursor']) # no more channels to iterate over else: @@ -284,13 +285,13 @@ def get_all_channel_objects(self, exclude_archived=True): return channels def get_all_user_objects(self): - url = self.url + "users.list?token=" + self.token + url = self.url + "users.list" return self.get_with_retry_to_json(url)['members'] def archive(self, channel_name): - url_template = self.url + "conversations.archive?token={}&channel={}" + url_template = self.url + "conversations.archive?channel={}" cid = self.get_channelid(channel_name) - url = url_template.format(self.token, cid) + url = url_template.format(cid) request = self.session.post(url) payload = request.json() return payload @@ -307,7 +308,6 @@ def post_message(self, channel, message, message_type=None): channel = channel[1:] post_data = { - 'token': self.token, 'channel': channel, 'text': message.encode('utf-8') }