Authentication consists of sending ArchivesSpace a username and password and receiving a session key in return.
Any interaction with the API after the initial authentication must then include that session key (in the header) until the session expires.
These slides discuss authenticating and then storing and sending the session key when using Python.
In order to authenticate with Postman, we posted our password to the user endpoint:
http://localhost:8089/users/admin/login
And we used our password as a parameter:
?password=secure_password
This assumes:
In real life, you may be using a base URL that is not the default. So instead of:
It might be more like:
Also in real life, you'll be using an ArchivesSpace staff account. I recommend creating one just for the API:
In which case this would be your user endpoint:
http://localhost:8089/users/api_user/login?password=squp6nusk1TIK
http://hogwarts-archives-api.org/users/api_user/login?password=squp6nusk1TIK
A collection of built-in modules. Simply put, they are some of the tools in your Python toolbox.
If you're a plumber, you use different tools than a carpenter and vice versa.
As someone interested in a RESTful web API that's sending and receiving JSON you'll use different Python libraries than a programmer working with mathmatical data sets or XML data.
ArchivesSnake is a Python library specifically designed for the ArchivesSpace API. Though I have an ArchivesSnake example, I am not using ASnake for these demonstrations.
Why not? This is very much a personal opinion, but I found that learning Requests first was easier, then I progressed to using ASnake. This is a beginners presentation, and as beginners you will need to learn Python as a broad tool first, then using that knowledge, progress towards the more specific goal of using ASnake for the Aspace API.
But if you already have some Python knowledge, want to start with ArchivesSnake, or simply disagree, go for ASnake!
#I have already installed these libraries, and here is where I call them
import requests
import json
auth = requests.post('http://localhost:8089/users/admin/login?password=secure_password').json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
print(json.dumps(auth, indent=2))
print('Your session key is: ' + session)
first_name = 'Fred'
last_name = 'Rogers'
print('Howdy neighbor! My name is ' + first_name + ' ' + last_name + ' and this is my neighborhood.')
Compare these:
('http://localhost:8089/users/admin/login?password=secure_password').json()
(baseURL + '/users/' + user + '/login?password=' + password).json()
#Variables
import requests
import json
baseURL = 'http://localhost:8089'
user = 'admin'
password = 'secure_password'
repository = '101'
# baseURL = 'http://sandbox.archivesspace.org/api'
# user = 'admin'
# password = 'admin'
auth = requests.post(baseURL + '/users/' + user + '/login?password=' + password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
print('Your session key is: ' + session + ' for ' + baseURL)
from asnake.client import ASnakeClient
client = ASnakeClient(baseurl='http://localhost:8089',
username='admin',
password='secure_password')
client.authorize()
print('Now your session key is: ' + client.authorize())
Which is resources/1
in my local install
#Back to using the Requests library
#headers=headers is where I'm storing the session key and delivering it each time I make a request
import requests
import json
baseURL = 'http://localhost:8089'
user = 'admin'
password = 'secure_password'
repository = '101'
auth = requests.post(baseURL + '/users/' + user + '/login?password=' + password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
print(headers)
output = requests.get(baseURL + '/repositories/' + repository + '/accessions/5', headers=headers).json()
print(json.dumps(output, indent=2))
Which is resources/101
in my local install
output = requests.get(baseURL + '/repositories/' + repository + '/resources/101', headers=headers).json()
print(json.dumps(output, indent=2))
# Final script all together.
# If you want to re-purpose this script, this is the block to use.
# Save it as name_of_file.py in order to run it as Python3 in your local environment.
# Disclaimer: This script is being provided as an example and may need to be modified for local use.
# Modifying and using this script is the responsibility of the individual using it.
# Do not test against Production!
# Authentication based on a script by ehanson8 (https://github.com/MITLibraries/archivesspace-api-python-scripts)
#Import the following libraries
import json
import requests
#Set your authentication info, baseurl, and repository info (if relevant)
baseURL = 'http://localhost:8089'
user = 'admin'
password = 'secure_password'
repository = '101'
#Authorize and store your session key in your header
auth = requests.post(baseURL + '/users/' + user + '/login?password=' + password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session, 'Content_Type': 'application/json'}
print('Your session key is: ' + session + ' for ' + baseURL)
#Modify this endpoint to GET and then print different record types
output = requests.get(baseURL + '/repositories/' + repository + '/resources/1', headers=headers).json()
print(json.dumps(output, indent=2))