Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Using the Users Service

Learning objectives
  • Learn how to build and deploy an App Engine app, a simple guestbook
Prerequisites
Related

Amy Unruh, Dan Sanderson, Oct 2012
Google Developer Relations

Introduction

Google App Engine provides several useful services based on Google infrastructure, accessible by applications using libraries included with the SDK. One such service is the Users service, which lets your application integrate with Google user accounts. This allows your users to sign in to your application using their existing Google accounts. This lesson teaches you how to incorporate the Users service into your App Engine application.

Using the Users Service

To personalize your application's greeting using the Users service, edit your helloworld/main.py file as follows:

import webapp2

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):
  def get(self):
    user = users.get_current_user()

    if user:
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + user.nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

Launch the application using the dev_appserver and reload its page (such as http://localhost:8080 ) in your browser. You'll be redirected to the local version of the Google sign-in page suitable for testing your application. You can enter any user name you'd like, and your application will see a fake User object based on that user name.

When your application is running on App Engine, users will be directed to the Google Accounts sign-in page, then redirected back to your application after successfully signing in or creating an account.

The Users API

Let's take a closer look at the new pieces:

user = users.get_current_user()

If the user is already signed in to your application, get_current_user() returns the User object for the user; otherwise, it returns None .

if user:
  self.response.headers['Content-Type'] = 'text/plain'
  self.response.out.write('Hello, ' + user.nickname())

If the user has signed in, display a personalized message, using the nickname associated with the user's account.

else:
  self.redirect(users.create_login_url(self.request.uri))

If the user has not signed in, tell webapp2 to redirect the user's browser to the Google Accounts sign-in screen. The redirect includes the URL back to this page ( self.request.uri ), so the Google Accounts sign-in mechanism will send the user back here after signing in or registering for a new account.

For more information, see the Users API documentation .

Summary and Review

Your application can now greet visiting users by name. Launch the application locally and try logging in and out.

In the next lesson , we'll add a feature to let users greet each other by using a form to submit guestbook messages.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.