34 lines
1.6 KiB
Python
34 lines
1.6 KiB
Python
import os
|
|
import requests
|
|
import json
|
|
from urllib.parse import urlencode
|
|
|
|
def generate_config(app_name, scopes):
|
|
#Ensure Credentials
|
|
parent = os.path.dirname(os.path.realpath(__file__))
|
|
with open(os.path.join(parent, 'config_template.json'), 'r') as template:
|
|
config = json.load(template)
|
|
|
|
#Create app
|
|
instance = input('Please enter your instance: ')
|
|
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()
|
|
|
|
client_id = config['client_id'] = response['client_id']
|
|
client_secret = config['client_secret'] = response['client_secret']
|
|
config['instance'] = instance
|
|
|
|
#Log in to user account
|
|
|
|
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})
|
|
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") |