a little cleanup
This commit is contained in:
		
							parent
							
								
									3c4dfb9f24
								
							
						
					
					
						commit
						5481b9e51d
					
				@ -14,6 +14,8 @@ username = os.getenv('USERNAME')
 | 
			
		||||
password = os.getenv('PASSWORD')
 | 
			
		||||
audience = os.getenv('AUDIENCE')
 | 
			
		||||
scope = os.getenv('SCOPE', '').split(',')
 | 
			
		||||
api_url = os.getenv('API_URL')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_token(client_id, client_secret, token_url, username, password, audience, scope):
 | 
			
		||||
    client = BackendApplicationClient(client_id=client_id)
 | 
			
		||||
@ -30,24 +32,16 @@ def get_token(client_id, client_secret, token_url, username, password, audience,
 | 
			
		||||
    return token
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def api_client(base_url, token):
 | 
			
		||||
    client = OAuth2Session(token=token)
 | 
			
		||||
    response = client.get(base_url)
 | 
			
		||||
def api_client(resource_url, auth_token):
 | 
			
		||||
    client = OAuth2Session(token=auth_token)
 | 
			
		||||
    response = client.get(resource_url)
 | 
			
		||||
    return response.content
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
client_id = '<your_client_id>'
 | 
			
		||||
client_secret = '<your_secret_id>'
 | 
			
		||||
token_url = '<oauth_token_url>'
 | 
			
		||||
username = '<your_username>'
 | 
			
		||||
password = '<your_password>'
 | 
			
		||||
audience = '<intended_audience>'
 | 
			
		||||
scope = ['scope1', 'scope2']
 | 
			
		||||
 | 
			
		||||
# Fetch token
 | 
			
		||||
token = get_token(client_id, client_secret, token_url, username, password, audience, scope)
 | 
			
		||||
 | 
			
		||||
# Now we can make API calls
 | 
			
		||||
api_response = api_client('https://api.example.com/resource', token)
 | 
			
		||||
api_response = api_client(api_url + '/resource', token)
 | 
			
		||||
 | 
			
		||||
print(api_response)
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
URL=https://api.dummy.com
 | 
			
		||||
API_URL=https://api.dummy.com
 | 
			
		||||
CLIENT_ID=client_id
 | 
			
		||||
CLIENT_SECRET=client_secret
 | 
			
		||||
TOKEN_URL=oauth_token_fetch_url
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user