...
Import requests, sys, getpass and requests.auth.HTTPBasicAuth to handle endpoint authentication.
Code Block language py import getpass import sys import requests from requests.auth import HTTPBasicAuth
Get user from input and password via getpass.getpass() so it's not prompted.
Code Block language py # input argument usr = sys.argv[1] # Prompt the user for a password without echoing. pwd = getpass.getpass()
Set the authentication endpoint
Code Block language py token_endpoint = 'https://vss-api.eis.utoronto.ca:8001/auth/request-token'
Make a POST request to token_request and create a new HTTPBasicAuth object initializing it with usr and pwd
Code Block language py r = requests.post(url=token_endpoint, auth=HTTPBasicAuth(usr, pwd))
Get JSON response body
Code Block language py data = r.json()
Print Error if any or the token if succeeded:
Code Block language py if r.ok: print 'TOKEN={}'.format(data['token']) else: print 'ERROR: {} {}: {}'.format(r.status_code, data['error'], data['message'])
...
Code Block | ||||
---|---|---|---|---|
| ||||
#!/usr/bin/env python
# importing modules
import getpass
import sys
import requests
from requests.auth import HTTPBasicAuth
# input argument
usr = sys.argv[1]
# Prompt the user for a password without echoing.
pwd = getpass.getpass()
# authentication endpoint
token_endpoint = 'https://vss-api.eis.utoronto.ca:8001/auth/request-token'
# making post request
r = requests.post(url=token_endpoint, auth=HTTPBasicAuth(usr, pwd))
data = r.json()
# getting token or printing error
if r.ok:
print 'TOKEN={}'.format(data['token'])
else:
print 'ERROR: {} {}: {}'.format(r.status_code, data['error'], data['message']) |
...