Google App Engine: the Users API

May 8, 2008

The previous versions of the online randomish numbers generator have been open to the entire web. The App Engine users API can be used to require a user login with a Google user account. Once again start by importing the corresponding code:

from google.appengine.api import users

In this case the password protected version of the online randomish number generator will be added to version 3 of the app. This app will be located at /3 and will call version3Page and starts by setting the value of user to the current user using the third line below.

class version3Page(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    if user:
      self.response.headers['Content-Type'] = 'text/html'
      self.response.out.write('<a href= target="_blank">')
      self.response.out.write('<b>Randomish Numbers</b></a> for ')
      self.response.out.write(user.nickname() + '.<p>')
      stop = 25
      i = 1
      while i <= stop:
        i = i + 1
      self.response.out.write('<a href=' + users.create_logout_url("/") +'>sign out</a><p>');

If the user is logged in then the randomish numbers are generated. The user is greeted by writing the value of user.nickname() to the page. Finally a link is added at the bottom so that the user can log out. This log out link is created with users.create_logout_url( “/” ) and the “/” will return the user to the top of the boldlentil app. If the user is not logged in then self.redirect(users.create_login_url(self.request.uri)) sends the user to a login screen to log in. Pretty straight forward.

The result is here.

The users API also has separate functions handle admin login so that, for example, certain pages can be accessed only by admins. The User Objects page notes that “at this time, the users API does not provide permanent unique identifiers for Google Accounts.” Although the same page also says that “unique user IDs and email address change propagation may be implemented in a future release.”


Leave a Reply

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

You are commenting using your 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: