#!/usr/bin/python import cgi import cgitb; cgitb.enable() # for troubleshooting import urllib import urllib2 br="
" # get code and state passed in dataIn = cgi.FieldStorage() code = dataIn.getvalue('code') state = dataIn.getvalue('state') topUrl = "https://api.toodledo.com/3/account/" tokenUrl = "https://api.toodledo.com/3/account/token.php" clientID = 'CLIENT_ID' # client ID goes here secret = 'SECRET' # Secret goes here tokenParams = { 'grant_type':'authorization_code', 'code':code, 'device':'python' } # Basic Authorization code from https://docs.python.org/2/howto/urllib2.html # create a password manager password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() # Add the username and password. password_mgr.add_password(None, topUrl, clientID, secret) auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = urllib2.build_opener(auth_handler) # Install the opener. # Now all calls to urllib2.urlopen use our opener. urllib2.install_opener(opener) ########################## # get the task list data = urllib.urlencode(tokenParams) req = urllib2.Request(tokenUrl, data) try: errortext = '' response = urllib2.urlopen(req) except urllib2.URLError as e: if hasattr(e, 'reason'): errortext += 'We failed to reach a server.
Reason: ' + str(e.reason) elif hasattr(e, 'code'): errortext += 'The server couldn\'t fulfill the request.
Error code: ' + str(e.code) print "Content-Type: text/html\n\n" # Need a couple of newlines after Content-Type to not get 500 Error print "TokenParams:", tokenParams, br if ( errortext ): print "Error:", errortext, br print "code: ", code, br, "state: ", state, br else: print "URL: ", response.geturl(), br print "Headers: ", response.info(), br print "Result: ", response.read(), br print "Done"