Skip to content
12 changes: 12 additions & 0 deletions endpoints/getting-started/Dockerfile.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM gcr.io/google_appengine/python

RUN apt-get update && \
apt-get install -y python2.7 python-pip && \
apt-get clean && \
rm /var/lib/apt/lists/*_*

ADD . /app
WORKDIR /app

RUN pip install -r requirements.txt
ENTRYPOINT ["gunicorn", "-b", ":8081", "main:app"]
167 changes: 167 additions & 0 deletions endpoints/getting-started/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Google Cloud Endpoints & Python

This sample demonstrates how to use Google Cloud Endpoints using Python.

For a complete walkthrough showing how to run this sample in different
environments, see the
[Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).

This sample consists of two parts:

1. The backend
2. The clients

## Running locally

### Running the backend

Install all the dependencies:
```bash
$ virtualenv env
$ source env/bin/activate
$ pip install -r requirements.txt
```

Run the application:
```bash
$ python main.py
```

### Using the echo client

With the app running locally, you can execute the simple echo client using:
```bash
$ python clients/echo-client.py http://localhost:8080 APIKEY helloworld
```

The `APIKEY` doesn't matter as the endpoint proxy is not running to do authentication.

## Deploying to Production

See the
[Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).

### Using the echo client

With the project deployed, you'll need to create an API key to access the API.

1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials).
2. Click 'Create credentials'.
3. Select 'API Key'.
4. Choose 'Server Key'

With the API key, you can use the echo client to access the API:
```bash
$ python clients/echo-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY helloworld
```

### Using the JWT client (with key file)

The JWT client demonstrates how to use a service account to authenticate to endpoints with the service account's private key file. To use the client, you'll need both an API key (as described in the echo client section) and a service account. To create a service account:

1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials).
2. Click 'Create credentials'.
3. Select 'Service account key'.
4. In the 'Select service account' dropdown, select 'Create new service account'.
5. Choose 'JSON' for the key type.

To use the service account for authentication:

1. Update the `google_jwt`'s `x-jwks_uri` in `swagger.yaml` with your service account's email address.
2. Redeploy your application.

Now you can use the JWT client to make requests to the API:
```bash
$ python clients/google-jwt-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/service-account.json
```

### Using the ID Token client (with key file)

The ID Token client demonstrates how to use user credentials to authenticate to endpoints. To use the client, you'll need both an API key (as described in the echo client section) and a OAuth2 client ID. To create a client ID:

1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials).
2. Click 'Create credentials'.
3. Select 'OAuth client ID'.
4. Choose 'Other' for the application type.

To use the client ID for authentication:

1. Update the `/auth/info/googleidtoken`'s `audiences` in `swagger.yaml` with your client ID.
2. Redeploy your application.

Now you can use the client ID to make requests to the API:
```bash
$ python clients/google-id-token-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/client-id.json
```

### Using the App Engine default service account client (no key file needed)

The App Engine default service account client demonstrates how to use the Google App Engine default service account to authenticate to endpoints.
We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com). The client project is running Google App Engine standard application.

To use the App Engine default service account for authentication:

1. Update the `gae_default_service_account`'s `x-issuer` and `x-jwks_uri` in `swagger.yaml` with your client project ID.
2. Redeploy your server application.
3. Update clients/service_to_service_gae_default/main.py, replace 'YOUR-CLIENT-PROJECT-ID' and 'YOUR-SERVER-PROJECT-ID' with your client project ID and your server project ID.
4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick.
```bash
$ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID
```

Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API using
the client's service account.

### Using the service account client (no key file needed)

The service account client demonstrates how to use a non-default service account to authenticate to endpoints.
We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com).
The client project is running Google App Engine standard application.

In the example, we use Google Cloud Identity and Access Management (IAM) API to create a JSON Web Token (JWT) for a service account, and use it to call an Endpoints API.

To use the client, you will need to enable "Service Account Actor" role for App Engine default service account:

1. Go to [IAM page](https://console.cloud.google.com/iam-admin/iam) of your client project.
2. For App Engine default service account, from “Role(s)” drop-down menu, select “Project”-“Service Account Actor”, and Save.

You also need to install Google API python library because the client code (main.py) uses googleapiclient,
which is a python library that needs to be uploaded to App Engine with your application code. After you run "pip install -t lib -r requirements",
Google API python client library should have already been installed under 'lib' directory. Additional information can be found
[here](https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#requesting_a_library).

To use the client for authentication:

1. Update the `google_service_account`'s `x-issuer` and `x-jwks_uri` in `swagger.yaml` with your service account email.
2. Redeploy your server application.
3. Update clients/service_to_service_non_default/main.py by replacing 'YOUR-SERVICE-ACCOUNT-EMAIL', 'YOUR-SERVER-PROJECT-ID' and 'YOUR-CLIENT-PROJECT-ID'
with your service account email, your server project ID, and your client project ID, respectively.
4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick.
```bash
$ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID
```

Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API using
the client's service account.

### Using the ID token client (no key file needed)

This example demonstrates how to authenticate to endpoints from Google App Engine default service account using Google ID token.
In the example, we first create a JSON Web Token (JWT) using the App Engine default service account. We then request a Google
ID token using the JWT, and call an Endpoints API using the Google ID token.

We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com).
The client project is running Google App Engine standard application.

To use the client for authentication:

1. Update the `google_id_token`'s audiences, replace `YOUR-SERVER-PROJECT-ID` with your server project ID.
2. Redeploy your server application.
3. Update clients/service_to_service_google_id_token/main.py, replace 'YOUR-CLIENT-PROJECT-ID' and 'YOUR-SERVER-PROJECT-ID' with your client project ID and your server project ID.
4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick.
```bash
$ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID
```

Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API from
the client's service account using Google ID token.
12 changes: 12 additions & 0 deletions endpoints/getting-started/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
runtime: python
vm: true
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
python_version: 3

beta_settings:
# Enable Google Cloud Endpoints API management.
use_endpoints_api_management: true
# Specify the Swagger API specification.
endpoints_swagger_spec_file: swagger.yaml
60 changes: 60 additions & 0 deletions endpoints/getting-started/clients/echo-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example of calling a simple Google Cloud Endpoint API."""

import argparse

import requests
from six.moves import urllib


def make_request(host, api_key, message):
"""Makes a request to the auth info endpoint for Google ID tokens."""
url = urllib.parse.urljoin(host, 'echo')
params = {
'key': api_key
}
body = {
'message': message
}

response = requests.post(url, params=params, json=body)

response.raise_for_status()
return response.text


def main(host, api_key, message):
response = make_request(host, api_key, message)
print(response)


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'host', help='Your API host, e.g. https://your-project.appspot.com.')
parser.add_argument(
'api_key', help='Your API key.')
parser.add_argument(
'message',
help='Message to echo.')

args = parser.parse_args()

main(args.host, args.api_key, args.message)
82 changes: 82 additions & 0 deletions endpoints/getting-started/clients/google-id-token-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python

# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example of calling a Google Cloud Endpoint API with an ID token obtained
using the Google OAuth2 flow."""

import argparse

import oauth2client.client
import oauth2client.file
import oauth2client.tools
import requests
from six.moves import urllib


def get_id_token(client_secrets_file, extra_args):
storage = oauth2client.file.Storage('credentials.dat')
credentials = storage.get()

if not credentials or credentials.invalid:
flow = oauth2client.client.flow_from_clientsecrets(
client_secrets_file, scope='email')
credentials = oauth2client.tools.run_flow(
flow, storage, flags=extra_args)

# The ID token is used by Cloud Endpoints, not the access token.
id_token = credentials.token_response['id_token']

return id_token


def make_request(host, api_key, id_token):
"""Makes a request to the auth info endpoint for Google ID tokens."""
url = urllib.parse.urljoin(host, '/auth/info/googleidtoken')
params = {
'key': api_key
}
headers = {
'Authorization': 'Bearer {}'.format(id_token)
}

response = requests.get(url, params=params, headers=headers)

response.raise_for_status()
return response.text


def main(host, api_key, client_secrets_file, extra_args):
id_token = get_id_token(client_secrets_file, extra_args)
response = make_request(host, api_key, id_token)
print(response)


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[oauth2client.tools.argparser])
parser.add_argument(
'host', help='Your API host, e.g. https://your-project.appspot.com.')
parser.add_argument(
'api_key', help='Your API key.')
parser.add_argument(
'client_secrets_file',
help='The path to your OAuth2 client secrets file.')

args = parser.parse_args()

main(args.host, args.api_key, args.client_secrets_file, args)
Loading