Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This quick how-to was written with python 2.7.11 and requests 2.8.1

PyVSS is now the supported and recommended method to interact with the RESTful API.

Step-by-step guide

The following steps demonstrate how get_token.py works:

  1. Import requests, sys, getpass and requests.auth.HTTPBasicAuth to handle endpoint authentication.

    import getpass
    import sys
    import requests
    from requests.auth import HTTPBasicAuth
  2. Get user from input and password via getpass.getpass() so it's not prompted.

    # input argument
    usr = sys.argv[1]
    # Prompt the user for a password without echoing.
    pwd = getpass.getpass()
  3. Set the authentication endpoint

    token_endpoint = 'https://vss-api.eis.utoronto.ca/auth/request-token'
  4. Make a POST request to token_request and create a new HTTPBasicAuth object initializing it with usr and pwd

    r = requests.post(url=token_endpoint, auth=HTTPBasicAuth(usr, pwd))
  5. Get JSON response body

    data = r.json()
  6. Print Error if any or the token if succeeded:

    if r.ok:
        print 'TOKEN={}'.format(data['token'])
    else:
        print 'ERROR: {} {}: {}'.format(r.status_code, data['error'], data['message'])

Script

Full python script is showing below:

#!/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/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'])

Download get_token.py 

  • No labels