Skip to page content Skip to main navigation
English | Site Directory

e.g. "templates" or "datastore"

# Google Accounts Python API Overview

App Engine applications can authenticate users with Google Accounts. An application can detect whether the current user has signed in with a Google Account, and can redirect the user to the Google Accounts sign-in page to sign in or create a new account. While a user is signed in to the application, the app can access the user's email address, as well as a unique user ID. The app can also detect whether the current user is an administrator, making it easy to implement admin-only areas of the app.

Google Accounts integration is an optional feature of App Engine. If you do not wish to integrate your app with Google Accounts, you can always build your own system for user authentication. Google Accounts offers a robust, easy to use authentication mechanism with a mature set of features and millions of active users.

- Using Google Accounts in Python
- Signing In and Out
- Accessing Account Information
- Users and the Datastore
- Google Accounts and the Development Server

## Using Google Accounts in Python

The following example greets a user that has signed in to the application with Google Accounts with a personalized message and a link to sign out. If the user is not signed in, the app offers a link to the Google Accounts sign-in screen.

    from google.appengine.api import users

    class MyHandler(webapp.RequestHandler):
      def get(self):
        user = users.get_current_user()
        if user:
          greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
                      (user.nickname(), users.create_logout_url("/")))
        else:
          greeting = ("<a href=\"%s\">Sign in or register</a>." %
                      users.create_login_url("/"))

        self.response.out.write("<html><body>%s</body></html>" % greeting)

### Enforcing Sign In and Admin Access With app.yaml

If you have pages that the user should not be able to access unless signed in, you can configure the handlers for those pages to require user sign-in with the `app.yaml` file. If a user accesses a URL configured to require sign-in and the user is not signed in, App Engine redirects the user to the Google Accounts sign-in page automatically, then directs the user back to the URL after signing in or registering successfully.

The handler configuration can also require that the user be a registered administrator for the application. This makes it easy to implement administrator-only sections of the site, without having to implement a separate authorization mechanism.

To learn how to configure authentication for URLs, see Configuring a Python App: Requiring Login or Administrator Status.

## Signing In and Out

An application can detect whether a user has signed in to the app with a Google Account. If the user is not signed in, the app can direct the user to Google Accounts to sign in or create a new Google account. The app gets the URL for the Google Accounts sign-in screen by calling a method of the Users API. The app can display this URL as a link, or it can issue an HTTP redirect to the URL when the user visits a page that requires authentication.

The Google Accounts sign-in screen informs the user that she is signing in to your application, using the application name you chose when registering the application. You can change your application name in the "Application Settings" section of the Admin Console.

Once the user has signed in or created a Google account, the user is redirected back to your application. The app provides the redirect URL to the method that generates the sign-in URL.

The Users API also includes a method to generate a URL for signing out of the app. The sign-out URL de-authenticates the user from the app, then redirects back to the app's URL without displaying anything.

A user is not signed in to an application until she is prompted to do so by the app and enters her account's email address and password. This is true even if the user has signed in to other applications using her Google Account.

## Accessing Account Information

While a user is signed in to an app, the app can access the account's email address for every request the user makes to the app. The app can also access a user ID and identifies the user uniquely, even if the user changes the email address for her account.

The app can also determine whether the current user is an administrator (a "developer") for the app. You can use this feature to build administrative features for the app, even if you don't use Google Accounts to authenticate other users. The Java and Python APIs make it easy to configure URLs as "administrator only."

## Users and the Datastore

The App Engine datastore supports storing the User object returned by the Google Accounts API as a special value type. As of this writing, User values do not behave as stable identifiers for users: if an app stores a User value and the user changes her email address, the User value will no longer refer to a valid user. In practice, users rarely change their Google Account email addresses, but it's worth designing for this rare case. A future update to the service may update User values in the datastore automatically. Until then, it is a best practice to not rely on the User value for stability.

The User object exposes a unique user ID that is guaranteed to be stable for the lifetime of the user's account, even after changing the email address. You can use this value in a datastore entity key or property value. If you wish to perform datastore queries over both email addresses and user IDs, you can store both the User object and the user ID from the object as separate properties.

## Google Accounts and the Development Server

The development server simulates the Google Accounts system using a dummy sign-in screen. When your application calls the Users API to get the URL for the sign-in screen, the API returns a special development server URL that prompts for an email address, but no password. You can type any email address into this prompt, and the app will behave as if you are signed in with an account with that address.

The dummy sign-in screen also includes a checkbox that indicates whether the dummy account is an administrator. If you check this box, the app will behave as if you are signed in using an administrator account.

Similarly, the Users API returns a sign-out URL that cancels the dummy sign-in.

The unique ID for a User object in the development server is calculated from the email address. Two unique email addresses always represent two unique users in the development server.