> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Secure Google Cloud Endpoints with Auth0

> Learn how to secure a Google Cloud API Endpoints with Auth0 access tokens.

[Google Cloud Endpoints (GCE)](https://cloud.google.com/endpoints/) is an API management system providing features to help you create, maintain, and secure your APIs. GCE uses [OpenAPI](https://www.openapis.org/) to define your API's endpoints, input and output, errors, and security description.

For more information on the OpenAPI spec, see the [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification) repository on GitHub.

This tutorial will cover how to secure Google Cloud Endpoints with Auth0.

## Prerequisites

Before you begin you'll need a deployed GCE API. If you haven't already created an API, complete the [Cloud Endpoints Quickstart](https://cloud.google.com/endpoints/docs/quickstart-endpoints) located in Google documentation.

The quickstart will walk you through creating a simple GCE API with a single endpoint, `/airportName`, that returns the name of an airport from its three-letter [IATA code](https://en.wikipedia.org/wiki/IATA_airport_code).

## Define the API in Auth0

Go to [Auth0 Dashboard > Applications > APIs](https://manage.auth0.com/#/apis), and create a new API.

![Dashboard - Create APIs - Integrations -Google Endpoints](https://mintlify.s3.us-west-1.amazonaws.com/docs-dev/images/cdy7uua7fh8z/7wUHnYBFp1jnbBurqoThpD/c3a31b64884c3adc2251a225a1ddd1dd/Create_API_-_EN.png)

Make note of the **API Audience (Audience: Unique identifier of the audience for an issued token. Named aud in a token, its value contains the ID of either an application (Client ID) for an ID Token or an API (API Identifier) for an Access Token.)** identifier (`http://google_api` in the screenshot above) to use in the following step.

## Update the API Configuration

Next, we'll update the OpenAPI configuration file for the GCE API. For the sample API created during the quickstart this file is `openapi.yaml`.

### Add Security Definitions

Open the configuration file and add a new `securityDefinitions` section. In this section, add a new definition (`auth0_jwt`) with the following fields:

| Field | Description |
| - | - |
| `authorizationUrl` | The authorization URL, should be set to `"https://{yourDomain}/authorize"` |
| `flow` | The flow used by the OAuth2 security scheme. Valid values are `"implicit"`, `"password"`, `"application"` or `"accessCode"`. |
| `type` | The type of the security scheme. Valid values are `"basic"`, `"apiKey"` or `"oauth2"` |
| `x-google-issuer` | The issuer of a credential, should be set to `"https://{yourDomain}/"` |
| `x-google-jwks_uri` | The URI of the public key set to validate the [JSON Web Token (JWT)](/docs/docs/glossary?term=JSON+Web+Token+%28JWT%29) signature. Set this to `"https://\{yourDomain}/.well-known/jwks.json"` |
| `x-google-audiences` | The API's identifier, make sure this value matches what you defined on the Auth0 dashboard for the API. |

### Update the Endpoint

Now, update the endpoint by adding a `security` field with the `securityDefinition` we created in the previous step.

```yaml lines theme={null}
paths:
  "/airportName":
    get:
      description: "Get the airport name for a given IATA code."
      operationId: "airportName"
      parameters:
        -
          name: iataCode
          in: query
          required: true
          type: string
      responses:
        200:
          description: "Success."
          schema:
            type: string
        400:
          description: "The IATA code is invalid or missing."
      security:
       - auth0_jwt: []
```

In the above example, the `security` field tells the GCE proxy that our `/airportName` path expects to be secured with the `auth0-jwt` definition.

After updating the OpenAPI configuration, it should look something like this:

### Redeploy the API

Next, redeploy your GCE API to apply the configuration changes. If you followed along with the [Cloud Endpoints Quickstart](https://cloud.google.com/endpoints/docs/quickstart-endpoints) you can redeploy by entering the following in Google's Cloud Shell:

```bash lines theme={null}
cd endpoints-quickstart/scripts
./deploy_api.sh
```

## Test the API

Once you've redeployed, call the API again with no security.

You'll get the following response:

```json lines theme={null}
{
 "code": 16,
 "message": "JWT validation failed: Missing or invalid credentials",
 "details": [
  {
   "@type": "type.googleapis.com/google.rpc.DebugInfo",
   "stackEntries": [],
   "detail": "auth"
  }
 ]
}
```

Which is exactly what we want!

Now go to the **Test** page of your Google Endpoints API definition on the [Auth0 Dashboard](https://manage.auth0.com/#/apis), and copy the Access Token (Access Token: Authorization credential, in the form of an opaque string or JWT, used to access an API.) under the Response:

Perform a `GET` request to your API with an Authorization Header of `Bearer {ACCESS_TOKEN}` to obtain authorized access:

And that's it!
