Crossdomain proxy on Google App Engine

Here’s a crossdomain proxy to pipe Javascript Ajax calls from you Google App Engine application to the Flickr API, since most browser will not let you call another domain directly.

import cgi
import urllib
from google.appengine.ext import webapp
from google.appengine.api import urlfetch

class FlickrController(webapp.RequestHandler):
	"""Proxy for Ajax calls to flickr"""
	def get(self):
		flickrapiendpoint = 'http://api.flickr.com/services/rest/'
		flickrapikey = 'you_flicker_api_key'
		
		params = self.request.GET
		params.add('api_key', flickrapikey)
		params.add('format', 'json')
		apiquery = urllib.urlencode(params)
		
		result = urlfetch.fetch(url=flickrapiendpoint + '?' + apiquery, method=urlfetch.GET)
		self.response.out.write(result.content)

def main():
	application = webapp.WSGIApplication(
		[('/flickr/', FlickrController)],
		debug=True)
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
	main()

I didn’t see examples of such a script anywhere else so I thought I’d post it here for all to see.

Leave a Reply

Your email address will not be published.