Skip to content

Instantly share code, notes, and snippets.

@will-moore
Last active July 26, 2016 09:03
Show Gist options
  • Save will-moore/36c0b75a6fcb787707c68caabc028d70 to your computer and use it in GitHub Desktop.
Save will-moore/36c0b75a6fcb787707c68caabc028d70 to your computer and use it in GitHub Desktop.
Using http://docs.python-requests.org/ to access OMERO via json api. For OMERO 5.2.x and earlier. See discussion at https://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8077
import requests
session = requests.Session()
BASE_URL = "https://test.openmicroscopy.org/omero/"
# BASE_URL = "http://localhost:4080/"
login_url = BASE_URL + "webclient/login/"
r = session.get(login_url)
token = r.cookies['csrftoken']
# Add token and referer to session header
session.headers.update({'X-CSRFToken': token,
'Referer': login_url})
# Login with username, password and server
payload = {'username': 'will',
'password': 'ome',
'server': 1}
r = session.post(login_url, data=payload)
projectId = None
datasetId = None
imageId = None
# list Projects
projects_url = BASE_URL + "webgateway/proj/list/"
r = session.get(projects_url)
projects = r.json()
for p in projects:
print p['id'], p['name']
if p['name'] == 'DVs':
projectId = p['id']
# list Datasets in Project
datasets_url = BASE_URL + "webgateway/proj/%s/children/" % projectId
r = session.get(datasets_url)
datasets = r.json()
for d in datasets:
if d['name'] == 'dv':
datasetId = d['id']
# list Images in Dataset
images_url = BASE_URL + "webgateway/dataset/%s/children/" % datasetId
r = session.get(images_url)
images = r.json()
for i in images:
if i['name'] == 'test.dv':
imageId = i['id']
# Get image data
image_url = BASE_URL + "webgateway/imgData/%s/" % imageId
r = session.get(image_url)
print r.json()
# Log out
logout_url = BASE_URL + "webclient/logout/"
r = session.post(logout_url)
print 'logout', r.status_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment