From cd0cbf647bfa3a6a1c4584be06377cdf4099cb5d Mon Sep 17 00:00:00 2001 From: Wardyn Date: Fri, 6 Oct 2023 12:02:22 -0700 Subject: [PATCH] rework generate_config to use a requests session --- generate_config.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/generate_config.py b/generate_config.py index b44691d..d51d315 100755 --- a/generate_config.py +++ b/generate_config.py @@ -5,6 +5,9 @@ import json from urllib.parse import urlencode def generate_config(app_name, scopes): + #Define Session + session = requests.Session() + #Ensure Credentials parent = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(parent, 'config_template.json'), 'r') as template: @@ -15,7 +18,7 @@ def generate_config(app_name, scopes): if not instance[:4] == 'http': instance = 'https://' + instance - response = requests.post(instance + '/api/v1/apps', data={'client_name':app_name,'scopes':scopes, 'redirect_uris':'urn:ietf:wg:oauth:2.0:oob'}).json() + response = session.post(instance + '/api/v1/apps', data={'client_name':app_name,'scopes':scopes, 'redirect_uris':'urn:ietf:wg:oauth:2.0:oob'}).json() client_id = config['client_id'] = response['client_id'] client_secret = config['client_secret'] = response['client_secret'] @@ -25,11 +28,11 @@ def generate_config(app_name, scopes): print(instance + '/oauth/authorize?', urlencode({'response_type':'code', 'client_id':client_id, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':scopes})) code = input("To generate a token to access your account, " + app_name + " needs an authorization code. Please authorize using the link above and enter the code it provides you \nCode: ") - response = requests.post(instance + '/oauth/token', data={'grant_type':'authorization_code', 'code':code, 'client_id':client_id, 'client_secret':client_secret, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':scopes}) + response = session.post(instance + '/oauth/token', data={'grant_type':'authorization_code', 'code':code, 'client_id':client_id, 'client_secret':client_secret, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':scopes}) config['user_token'] = response.json()['access_token'] with open(os.path.join(parent, 'config.json'), 'w') as config_file: config_file.write(json.dumps(config)) if __name__ == "__main__": - generate_config("test_app", "read") \ No newline at end of file + generate_config("test_app", "read write follow push") \ No newline at end of file