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 = "https://api.del.icio.us/rename?new=bla1&old=bla2"

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:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14",
"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 del.icio.us tag cleaner.

1 comment:

archaeology - NDSU 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!

Aaron