rework generate_config to use a requests session

This commit is contained in:
Wardyn 2023-10-06 12:02:22 -07:00
parent 06b7b9f698
commit cd0cbf647b
1 changed files with 6 additions and 3 deletions

View File

@ -5,6 +5,9 @@ import json
from urllib.parse import urlencode from urllib.parse import urlencode
def generate_config(app_name, scopes): def generate_config(app_name, scopes):
#Define Session
session = requests.Session()
#Ensure Credentials #Ensure Credentials
parent = os.path.dirname(os.path.realpath(__file__)) parent = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(parent, 'config_template.json'), 'r') as template: 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': if not instance[:4] == 'http':
instance = 'https://' + instance 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_id = config['client_id'] = response['client_id']
client_secret = config['client_secret'] = response['client_secret'] 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})) 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: ") 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'] config['user_token'] = response.json()['access_token']
with open(os.path.join(parent, 'config.json'), 'w') as config_file: with open(os.path.join(parent, 'config.json'), 'w') as config_file:
config_file.write(json.dumps(config)) config_file.write(json.dumps(config))
if __name__ == "__main__": if __name__ == "__main__":
generate_config("test_app", "read") generate_config("test_app", "read write follow push")