Erik's blog

Code, notes, recipes, general musings

App Engine Y!AP app that pushes updates via OpenSocial JS API

leave a comment »

Usage:

  1. create App Engine app
  2. edit main.py to look like the code below and deploy
  3. create YAP app
  4. set app base url to yourappname.appspot.com/example
  5. preview your app
import wsgiref.handlers
from google.appengine.ext import webapp

class ExampleHandler(webapp.RequestHandler):
	def post(self):
		html = """
		<script>
		//ref: http://developer.yahoo.com/yap/guide/opensocial-examples.html
		var postActivity = function(title, body) {
				var params = {};
				params[opensocial.Activity.Field.TITLE] = title;
				params[opensocial.Activity.Field.BODY] = body;
				var activity = opensocial.newActivity(params);
				opensocial.requestCreateActivity(
					activity,
					opensocial.CreateActivityPriority.LOW,
					function(){});
			},
			handleResponse = function(response){

				var viewer = response.get('viewer').getData(),
					name = viewer.getDisplayName();

				postActivity(
					name + ' posted an update ...',
					'... using OpenSocial!'
				);
			},
			getViewerData = function() {
				var req = opensocial.newDataRequest();
			  	req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
				req.send(handleResponse);
			};

		//this is the bare minimum code to push updates
		var params = {};
		params[opensocial.Activity.Field.TITLE] = 'title';
		params[opensocial.Activity.Field.BODY] = 'body';
		var activity = opensocial.newActivity(params);
		opensocial.requestCreateActivity(
			activity,
			opensocial.CreateActivityPriority.LOW,
			function(){});

		//this is a slightly enhanced update flow
		getViewerData();

		</script>
		"""
		self.response.headers['Content-Type'] = 'text/html'
		self.response.out.write(html)

application = webapp.WSGIApplication(
	[('/example', ExampleHandler)],
	debug=True)

def main():
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
 main()
Advertisements

Written by Erik

December 19, 2008 at 11:56 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: