Google App Engine
IMified user Barry sent us this code for building an IMified bot in Python on Google App Engine. This code can also be used for an example of how to perform HTTP Authentication with Python.
# Bot request from IMIfied service.
class BotRequest(webapp.RequestHandler):
def handle_request(self):
step = int(self.request.get('step', default_value=0))
userkey = self.request.get('userkey')
msg = self.request.get('msg')
value1 = self.request.get('value1')
url = "https://www.imified.com/api/bot/"
form_fields = {
"botkey": "XXXXXXXX-XXX-XXXXXX", # Your bot key goes here.
"apimethod": "getuser", # the API method to call.
"userkey": str(userkey) # User Key to lookup with getuser.
}
# Build the Basic Authentication string. Don't forget the [:-1] at the end!
base64string = base64.encodestring('%s:%s' % ('youname@email.com', 'password'))[:-1]
authString = 'Basic %s' % base64string
# Build the request post data.
form_data = urllib.urlencode(form_fields)
# Make the call to the service using GAE's urlfetch command.
response = urlfetch.fetch(url=url, payload=form_data, method=urlfetch.POST, headers={'AUTHORIZATION' : authString})
# Check the response of 200 and process as needed.
if response.status_code == 200:
self.response.out.write("Worked:Status Code 200.<br>")
self.response.out.write(response.content)
else:
self.response.out.write("Did not work.<br>[)
self.response.out.write(]("))
self.response.out.write(response.headers)
self.response.out.write(response.status_code)
def get(self):
self.handle_request()
def post(self):
self.handle_request()
The original help thread is here