Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This quick how-to was written with python 23.710.11 8 and requests 2.828.10

Note

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

Step-by-step guide

The following steps demonstrate how get_token.py workshow to get an access token:

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

    Code Block
    languagepy
    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.

    Code Block
    languagepy
    # input argument
    usr = sys.argv[1]
    # Prompt the user for a password without echoing.
    pwd = getpass.getpass()


  3. Set the authentication endpoint

    Code Block
    languagepy
    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

    Code Block
    languagepy
    # NO MFA
    r = requests.post(url=token_endpoint, auth=HTTPBasicAuth(usr, pwd))
    # With MFA
    r = requests.post(url=token_endpoint, auth=HTTPBasicAuth(usr, pwd), json={'otp': '00000'})
  5. Get JSON response body

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

    Code Block
    languagepy
    if r.ok:
        print

...

  1. (f"TOKEN={

...

  1. data['token']}")
    else:
        print

...

  1. (f"ERROR: {

...

  1. r.status_code

...

  1. } {data['

...

  1. type']

...

  1. }: {data['message']}")

Script

Full python script is showing below:

Code Block
languagepy
collapsetrue
#!/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 '(f"TOKEN={}'.format(data['token']}")
else:
    print 'ERROR: {} {}(f"ERROR: {}'.format(r.status_code,} {data['errortype'],}: {data['message']}")

Download get_token.py 

Filter by label (Content by label)
showLabelsfalse
max5
spacesAPI
sortmodified
showSpacefalse

...

reversetrue
typepage
cqllabel = "kb-how-to-article" and type = "page" and space = "API"
labelskb-how-to-article
Page Properties
hiddentrue


Related issues