Saturday, May 31, 2008

Basic and digest HTTP authentication when using Google App Engine URL fetch API

When using Google AppEngine's URL fetch API it's not obvious how to do a basic digest HTTP/S authentication, so here's a self-explainable code snippet for doing so:

import base64

username = "johnsmith"
pw = "cookie1234"

encoded = base64.b64encode(username + ':' + pw)
authstr = "Basic "+encoded

url = ""

mheaders = {'Authorization':authstr,}

result = urlfetch.fetch(url,headers = mheaders)

Other useful HTTP request headers you could add to the mheaders dict above are:

"User-Agent" : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20080404 Firefox/",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive": "300",
"Connection": "keep-alive",
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"Cache-Control": "max-age=0",
both of these were very handy when implementing the tag cleaner.


Unknown said...

Thanks! This post was a big help. I was trying to use the fetch API with google app engine, and kept getting a cached version of my content back... I had updated it several times and couldn't figure out where it was cached. Your post helped me figure out how to set the caching header for the fetch request.

Thanks again!


shreekavi said...
This comment has been removed by a blog administrator.