Skip to content

Instantly share code, notes, and snippets.

@manning-ncsa
Created October 12, 2022 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manning-ncsa/9ac2084eb8063b05e96735bdd3fdba2e to your computer and use it in GitHub Desktop.
Save manning-ncsa/9ac2084eb8063b05e96735bdd3fdba2e to your computer and use it in GitHub Desktop.

Basic DESaccess login via API

To test the DESaccess login API and general programmatic access pattern, try the following. Define environment variables as shown, inserting your username and password. Then run the Python script to test that you can authenticate and fetch an API access token.

export DESACCESS_API_BASEURL=https://des.ncsa.illinois.edu
export DESACCESS_API_DB=desdr
export DESACCESS_API_USER=
export DESACCESS_API_PASS=

python desaccess_api.py
#!/usr/bin/env python3
import os
import sys
import requests
import time
import json
try:
username = os.environ['DESACCESS_API_USER']
password = os.environ['DESACCESS_API_PASS']
base_url = os.environ['DESACCESS_API_BASEURL']
db = os.environ['DESACCESS_API_DB']
# Import credentials and config from environment variables
config = {
'auth_token': '',
'apiBaseUrl': '{}/desaccess/api'.format(base_url),
'filesBaseUrl': '{}/files-desaccess/'.format(base_url),
'username': username,
'password': password,
'database': db,
}
except:
sys.exit(1)
def login():
"""Obtains an auth token using the username and password credentials for a given database.
"""
# Login to obtain an auth token
r = requests.request( 'POST',
'{}/login'.format(config['apiBaseUrl']),
data={
'username': config['username'],
'password': config['password'],
'database': config['database']
},
# verify=False
)
# Store the JWT auth token
config['auth_token'] = r.json()['token']
return config['auth_token']
if __name__ == '__main__':
# Authenticate and store the auth token for subsequent API calls
try:
print('Logging in as user "{}" ("{}") and storing auth token...'.format(config['username'], config['database']))
login()
except:
print('Login failed.')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment