Skip to content

Instantly share code, notes, and snippets.

@olsnacky
Last active December 21, 2015 22:52
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 olsnacky/5e45e3af49055a4eaaaf to your computer and use it in GitHub Desktop.
Save olsnacky/5e45e3af49055a4eaaaf to your computer and use it in GitHub Desktop.
Working code example of working with Imagus facial recognition API
import urllib, urllib2, json, cookielib
# the api's base url
BASE_URL = 'https://api.imagus.com.au/api/v1/'
# our credentials
USER_ID = 'xxxxx'
USER_PASSWORD = 'xxxxx'
# get the various login urls
LOGINS_URL = '%slogins' % (BASE_URL)
response = urllib2.urlopen(LOGINS_URL)
data = json.load(response)['logins']
# this method will build any request that requires authorisation/authentication
# it adds our captured cookie to the request
# we'll use this method when we need to use endpoints that require authentication/authorisation
def build_authorised_request(url, cookie):
request = urllib2.Request(url)
request.add_header('Cookie', cookie)
return request
# find the login url that suits our needs
auth_url = None
LOGIN_TYPE = 'api'
LOGIN_NAME = 'imagus'
for login in data:
if login['type'] == LOGIN_TYPE and login['name'] == LOGIN_NAME:
auth_url = login['auth_url']
# if we have the right auth url, we're in business
if auth_url is not None:
# log in the user
auth_url = '%s&username=%s&password=%s' % (auth_url, USER_ID, USER_PASSWORD)
response = urllib2.urlopen(auth_url)
# get the user's cookie
cookie = response.info()['Set-Cookie']
# show the user is logged in
# we need to pass the cookie for authorisation/authentication
USER_URL = '%suser' % (BASE_URL)
response = urllib2.urlopen(build_authorised_request(USER_URL, cookie))
data = json.load(response)
print('Logged in as: %s %s' % (data['first_name'], data['second_name']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment