Created
April 4, 2020 07:41
-
-
Save valeriocos/de31324625a3fab32449cf5d43b24075 to your computer and use it in GitHub Desktop.
Get a OAuth2 token for Slack app using requests in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright (C) 2015-2020 Bitergia | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# Authors: | |
# Valerio Cosentino <valcos@bitergia.com> | |
# | |
import json | |
import requests | |
OAUTH_AUTHORIZE_URL = "https://slack.com/oauth/v2/authorize?scope={}&client_id={}&redirect_uri={}&state={}" | |
OAUTH2_TOKEN_URL = 'https://slack.com/api/oauth.v2.access?client_id={}&client_secret={}&redirect_uri={}&code={}' | |
SCOPES = "channels:history,channels:read,users:read" | |
def get_token_info(client_id, client_secret, redirect_uri, code): | |
access_uri = OAUTH2_TOKEN_URL.format(client_id, client_secret, redirect_uri.strip("\""), code) | |
headers = { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Accept': 'application/json' | |
} | |
r = requests.post(access_uri, headers=headers) | |
r_json = r.json() | |
return r_json | |
def main(): | |
"""This script assists the user to generate an OAuth2 token | |
to access the Slack API. It requires the package requests. | |
The information needed by the script is available in the | |
App workspace: https://api.slack.com/apps | |
An example of execution is provided below: | |
Enter your client ID: xxx.xxx | |
Redirect URI (between quotes): "https://bitergia.com/" | |
Enter a verification code: xxx | |
Access the URL below, authorize the application, and get the code that appears | |
on the URL. Make sure that the state value matches the value you passed. | |
https://slack.com/oauth/v2/authorize?scope=... | |
Enter the code: ... | |
Enter your client secret: ... | |
***** token ***** | |
{ | |
"access_token": "...", | |
"app_id": "...", | |
"authed_user": { | |
"id": "..." | |
}, | |
"bot_user_id": "...", | |
"enterprise": null, | |
"ok": true, | |
"scope": "channels:history,channels:read,users:read", | |
"team": { | |
"id": "...", | |
"name": "..." | |
}, | |
"token_type": "bot" | |
} | |
""" | |
client_id = input('Enter your client ID: ') | |
redirect_uri = input('Redirect URI (between quotes): ') | |
state = input('Enter a verification code: ') | |
url = OAUTH_AUTHORIZE_URL.format(SCOPES, client_id, redirect_uri.strip("\""), state) | |
print("\nAccess the URL below, authorize the application, and get the code that appears " | |
"on the URL. Make sure that the state value matches the value you passed.") | |
print(url + "\n") | |
code = input('Enter the code: ') | |
client_secret = input('Enter your client secret: ') | |
info = get_token_info(client_id, client_secret, redirect_uri, code) | |
print("\n***** token *****") | |
print(json.dumps(info, sort_keys=True, indent=4)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment