# Get the API key by making a Box application here: http://box.com/developers/services/edit/

api_key ='replace-with-api-key'

importos

importsys

importurllib

importurllib2

import MultipartPostHandler,cookielib# MultipartPostHandler can be found at: http://pypi.python.org/pypi/MultipartPostHandler2

fromxml.domimport minidom

def authenticate(api_key):

xml_ticket = minidom.parseString(urllib.urlopen('https://www.box.com/api/1.0/rest?action=get_ticket&api_key='+api_key).read())# This grabs the XML authentication ticket that we'll be using to generate the auth_token.

status = xml_ticket.firstChild.childNodes[0].firstChild.data# This gets the ticket status to make sure we actually retrieved a valid ticket.

if status =='get_ticket_ok': # Did we get a valid ticket? If so, continue. If not, the user needs to check their API key.

ticket = xml_ticket.firstChild.childNodes[1].firstChild.data

print'Please go to https://www.box.com/api/1.0/auth/'+ticket+' and follow the instructions there. Then, press ENTER or RETURN to continue.'

raw_input()

xml_auth_token = minidom.parseString(urllib.urlopen('https://www.box.net/api/1.0/rest?action=get_auth_token&api_key='+api_key+'&ticket='+ticket).read())# This is the auth token in XML format.