Google Cloud Platform

Using the Users Service

Google App Engine provides several useful services based on Google infrastructure. One example is the Users service, which lets your application integrate with Google user accounts. With the Users service, your users can employ the Google accounts they already have to sign in to your application.

The Users service makes it easy to personalize this application's greeting.

Using Users

We're going to build on the Hello, World! application. You can clone the code for this step from this branch on GitHub, or if you already have the original Hello, World! app, replace its contents with the following:

Test the application in your browser. When running on the local development server, your application displays the user, which is the only user supported when running on the local development server.

When your application is running on production App Engine, however, 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:

The appengine.NewContext function returns a context.Context value associated with the current request. This is an opaque value used by many functions in the Go App Engine SDK to communicate with the App Engine service.

If the user is already signed in to your application, user.Current returns a pointer to a user.User value for the user. Otherwise, it returns nil.

If the user has not signed in, redirect the user's browser to the Google account sign-in screen by setting a Location header and returning an HTTP status code of 302 "Found". The redirection includes the URL to this page (r.URL.String()) so the Google account sign-in mechanism will send the user back here after signing in or registering for a new account.

The user.LoginURL function returns an error value as its second argument. Though an error is unlikely to occur here, it is good practice to check it and display an error user, if appropriate (in this case, with the http.Error helper).

If the user has signed in, display a personalized message using the name associated with the user's account. In this case, the fmt.Fprintf function calls *user.User's String method to get the user's name in string form.

For more information about the Users API, see the Users reference.

Note: for specific paths that should be served only to logged in users, use the login: required directive in the app.yaml file. See the app.yaml reference for details.


Our application can now greet visiting users by name. Let's add a feature that will let users greet each other.

Handling Forms >>