## Prerequisites

This page assumes that you have already:

- [Created a Google Cloud project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt).

- [Added API management](https://docs.cloud.google.com/endpoints/docs/frameworks/java/adding-api-management.md.txt).

## Configuring authentication

To authenticate with a service account:

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

   ```python
   import endpoints
   ```
2. Add an issuer object for the service account to the
   [API decorator](https://docs.cloud.google.com/endpoints/docs/frameworks/python/decorators-reference.md.txt#defining_the_api_endpointsapi).
   For example:

   ```
   @endpoints.api(
       name='echo',
       version='v1',
       issuers={'serviceAccount': endpoints.Issuer(
           'YOUR_SERVICE_ACCOUNT_EMAIL',
           'https://www.googleapis.com/robot/v1/metadata/x509/YOUR_SERVICE_ACCOUNT_EMAIL')},
       audiences={'serviceAccount': ['YOUR_AUDIENCE']})
   ```
   - Replace `echo` with the name of your API.
   - Replace `v1` with your API version.
   - Replace `YOUR_SERVICE_ACCOUNT_EMAIL` with your service account email.
   - Replace `YOUR_AUDIENCE` with the value in the `aud` field sent by the calling service.
3. In each API method where you want to check for proper authentication,
   check for a valid `User` and raise error `401`if there isn't one, as
   shown in this sample method definition:

   ```python
   user = endpoints.get_current_user()
   # If there's no user defined, the request was unauthenticated, so we
   # raise 401 Unauthorized.
   ```
4. [Deploy the API](https://docs.cloud.google.com/endpoints/docs/frameworks/python/test-deploy.md.txt). You need to
   redeploy the API whenever you add new clients.