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 Auth with Endpoints

Requirements: a Google API project

The instructions on this page assume that you have a project for your application in the Google Developers Console . If you don't have one yet, here's how to create one:

Go to the Google Developers Console . Click Create Project , enter a name and a project ID, or accept the defaults, and click Create .

Adding authorization to an API backend

If you wish to protect all or part of your API by authorization, you must:

  1. Specify the client IDs ( allowed_client_ids ) allowed to make requests.
  2. Add a User Check to all exposed methods to be protected by authorization.
  3. Redeploy the API backend.
  4. Generate the client library again.

Specifying authorized clients in the API backend

You must specify which clients are allowed to access the backend by means of a list of client IDs . A client ID is generated by the Google Developers Console from a client secret, such as the SHA1 fingerprint of a key used to secure an Android app, or from the Bundle ID/Apple Store ID pair for an iOS app, as described in Creating OAuth 2.0 Client IDs . At runtime, a client app is granted the authorization token it needs to send requests to the backend if its client secret matches one contained in a client ID within the backend's client ID list.

To specify which clients are authorized to access your API backend:

  1. Get a complete list of the client IDs of the clients that you want to grant access to. This list is a list of OAuth 2.0 client IDs obtained for your project following the instructions provided below under Creating OAuth 2.0 Client IDs .
  2. In the allowed_client_ids argument of the @endpoints.api decorator or @endpoints.method decorator for your API, supply the list of client IDs that you want to authenticate.
    • For an iOS app, supply its iOS client ID in the allowed_client_ids whitelist.
    • For a javascript app, supply its web client ID in the allowed_client_ids whitelist.
    • For an Android app, supply both its Android client ID and a web client ID in allowed_client_ids whitelist. (You must add that same web client ID to the audiences list as shown next.)
  3. If you have an Android client, you must also supply the audiences argument for the @endpoints.api decorator, set to the web client ID mentioned in the preceding step. The same web client ID must be in both allowed_client_ids and audiences .

Adding a user check to methods

The API backend framework automatically authenticates the user and enforces the allowed_client_ids whitelist of authorized clients, by supplying a valid User or not in the API backend, which you can obtain using endpoints.get_current_user() . If the request from the app has a valid auth token or is in the list of allowed_client_ids , the framework supplies a valid User . If the incoming request does not have a valid auth token or if the client is not on the allowed_client_ids , the framework sets the User to null. Your own code must handle the null case and the non-null case, as shown below.

To require user authorization for a method, you need to check for the existence of a valid User obtained from the incoming request. To do this:

  1. Import the App Engine Endpoints API in your API class:

    from google.appengine.ext import endpoints
    
  2. Inside the method, get the current authorized user by invoking endpoints.get_current_user() in your method. If the incoming method has a valid auth or ID token, endpoints.get_current_user() returns a User , otherwise it returns None .

  3. If there is no valid User, raise the exception endpoints.UnauthorizedException , as shown in the following snippet:

    current_user = endpoints.get_current_user()
        if raise_unauthorized and current_user is None:
            raise endpoints.UnauthorizedException('Invalid token.')
    

Supporting authorization in clients

If your API backend requires authentication, you need to also provide support for authorization in your Android, iOS, or JavaScript client. Instructions for this vary slightly by client type, so more information is provided in these client-specific pages:

Creating OAuth 2.0 client IDs

If you wish to require authorization to access your API backend, you must obtain the required client IDs and supply them to the backend using the proper API decorator argument. Precisely which client IDs are required and how you need to supply them can vary depending on whether the client is an Android app, iOS app, or javascript app. For details, see Specifying Authorized Clients in the API Backend .

Creating a web client ID

To create a web client ID:

  1. Visit the Google Developers Console in your browser.
  2. Select the project you are using for your backend API from the projects pulldown menu.
  3. Select APIs & auth > Credentials > Create new Client ID .
  4. Fill out the Create Client ID form as follows:
    1. Select Web application as the Application Type .
    2. If you are testing the backend locally , specify http://localhost:8080 in the textbox labeled AUTHORIZED JAVASCRIPT ORIGINS . If you are deploying your backend API to production App Engine, specify the App Engine URL of your backend API in the textbox labeled AUTHORIZED JAVASCRIPT ORIGINS ; for example, https://your_project_id.appspot.com , replacing your_project_id with your actual project ID.
    3. Click Create Client ID .
    4. Note the client ID as you'll need to use it later in your client code. (Alternatively, you can revisit the project later in the console to determine its client ID.)

Creating an Android client ID

In order to create the Android client ID, you'll need to have the SHA1 fingerprint of the key you are using. If you use Eclipse with the Android Developer Tools (ADT) plugin, a debug keystore and a debug key is created automatically. You can use the debug key for testing purposes. (You must use a release key for your release version!) These instructions assume you are using the debug key.

The default debug keystore password is android , and the key alias is androiddebugkey . The default location for Linux and Mac OSX is ~/.android .

  1. Generate a debug (or release) key for your Android application, if you don’t already have one. If you use Eclipse with the Android Developer Tools plugin, Eclipse automatically generates a debug key in the debug keystore the first time you build an Android project.
  2. In a Linux or Mac OSX terminal window, you can get the SHA1 fingerprint of the debug key using the keytool (included with the Java SDK) and OpenSSL as follows:
    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1
    When prompted for a keystore password, for debug , enter the value android .
  3. Copy the SHA fingerprint key that is displayed after your run the above keytool command. You'll need to supply this when you generate the Android client ID in the console.
  4. Open the Google Developers Console .
  5. Select your project in the projects pulldown menu.
  6. Select APIs & auth > Credentials > Create new Client ID .
  7. Fill out the Create Client ID form as follows:
    1. For Application type , select Installed application > Android
    2. In the textbox labeled Package name enter your Android application package name.
    3. In the textbox labeled SIGNING CERTIFICATE FINGERPRINT , enter the SHA1 fingerprint you obtained above.
    4. Click Create Client ID .
  8. Note the client ID as you'll need to use it later in your client code. (Alternatively, you can revisit the project later in the console to determine its client ID.)

Creating an iOS client ID

  1. Open the Google Developers Console .
  2. When your project appears in the projects pulldown menu, select it to make it the active project.
  3. Select APIs & auth > Credentials > Create new Client ID .
  4. Fill out the Create Client ID form as follows:
    1. For Application type , select Installed application > iOS
    2. In the textbox labeled Bundle ID enter your application’s bundle identifier as listed in your application's .plist file (e.g. com.example.myapp ).
    3. In the textbox labeled App Store ID , enter the App Store ID if the app was published in the Apple iTunes® App Store.
    4. Click Create Client ID .
  5. Note the client ID as you'll need to use it later in your client code. (Alternatively, you can revisit the project later in the console to determine its client ID.)

Authentication required

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

Signing you in...

Google Developers needs your permission to do that.