ESP を使用した App Engine フレキシブル環境用 Cloud Endpoints のスタートガイド

このチュートリアルでは、サンプル API と Extensible Service Proxy(ESP)App Engine フレキシブル環境内のインスタンスで実行するように構成してデプロイする方法を説明します。サンプル API は、OpenAPI 仕様を使用して作成されています。このチュートリアルでは、API キーを作成して API へのリクエストで使用する方法も説明します。

Cloud Endpoints の概要については、Endpoints についてEndpoints アーキテクチャをご覧ください。

目標

タスクの概要を示す次のリストを参照しながら、チュートリアルを実施してください。API にリクエストを送信するには、すべてのタスクを行う必要があります。

  1. Google Cloud プロジェクトを設定し、必要なソフトウェアをインストールして App Engine アプリを作成します。始める前にをご覧ください。
  2. サンプルコードをダウンロードします。サンプルコードを取得するをご覧ください。
  3. Endpoints の構成に使用する openapi-appengine.yaml ファイルを構成します。Endpoints を構成するをご覧ください。
  4. Endpoints 構成をデプロイして、Endpoints サービスを作成します。Endpoints 構成をデプロイするをご覧ください。
  5. API と ESP を App Engine にデプロイします。API バックエンドをデプロイするをご覧ください。
  6. API にリクエストを送信します。API にリクエストを送信するをご覧ください。
  7. API の活動を追跡します。API の活動を追跡するをご覧ください。
  8. Google Cloud アカウントに課金されないようにします。クリーンアップをご覧ください。

費用

このドキュメントでは、課金対象である次の Google Cloudコンポーネントを使用します。

料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。

新規の Google Cloud ユーザーは無料トライアルをご利用いただける場合があります。

このドキュメントに記載されているタスクの完了後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

  6. 後で必要になるため、プロジェクト ID をメモしておきます。
  7. サンプル API にリクエストを送信するためのアプリケーションが必要です。

    • Linux ユーザーと MacOS ユーザーの場合: このチュートリアルでは、curl の使用例を示します。これは通常、オペレーティング システムにプリインストールされています。 curl を所有していない場合は、curlリリースとダウンロードのページからダウンロードできます。
    • Windows ユーザーの場合: このチュートリアルでは、Invoke-WebRequest の使用例を示しています。これは PowerShell 3.0 以降でサポートされています。
  8. Google Cloud CLI をダウンロードします
  9. gcloud CLI を更新し、Endpoints コンポーネントをインストールします。
    gcloud components update
  10. Google Cloud CLI(gcloud)が、 Google Cloudにある対象のデータとサービスへのアクセスが許可されていることを確認します。
    gcloud auth login
    表示された新しいブラウザタブで、アカウントを選択します。
  11. デフォルト プロジェクトを実際のプロジェクト ID に設定します。
    gcloud config set project YOUR_PROJECT_ID

    YOUR_PROJECT_ID を実際のプロジェクト ID に置き換えます。他にも Google Cloud プロジェクトがあり、gcloud を使用してそれらのプロジェクトを管理する場合は、 gcloud CLI 構成の管理をご覧ください。

  12. App Engine アプリケーションを作成するリージョンを選択します。次のコマンドを実行してリージョンのリストを取得します。
    gcloud app regions list
  13. App Engine アプリケーションを作成します。YOUR_PROJECT_ID は実際の Google Cloudプロジェクト ID に、YOUR_REGION は App Engine アプリケーションを作成するリージョンに置き換えます。
      gcloud app create \
      --project=YOUR_PROJECT_ID \
      --region=YOUR_REGION
    
  14. App Engine のデフォルト サービス アカウントに編集者(roles/editor)ロールがない場合は、次の権限を付与します。
    • サービスレベルで App Engine のデフォルト サービス アカウントに roles/servicemanagement.serviceConsumer を付与します。
    • プロジェクト レベルで App Engine のデフォルトのサービス アカウントに roles/servicemanagement.serviceController を付与します。

    サービス アカウントへのロールの付与については、 Google Cloud コンソールを使用したアクセスの管理をご覧ください。

サンプルコードの取得

Java

サンプル API のクローンを作成するか、ダウンロードするには:

  1. サンプルコードでは Maven を使用しています。Maven 3.3.9 以降がインストールされていない場合には、ダウンロードしてインストールします。
  2. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    git clone https://github.com/GoogleCloudPlatform/java-docs-samples

    または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。

  3. サンプルコードが含まれているディレクトリに移動します。
    cd java-docs-samples/endpoints/getting-started
Python

サンプル API のクローンを作成するか、ダウンロードするには:

  1. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    git clone https://github.com/GoogleCloudPlatform/python-docs-samples

    または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。

  2. サンプルコードが含まれているディレクトリに移動します。
    cd python-docs-samples/endpoints/getting-started
Go

サンプル API のクローンを作成するか、ダウンロードするには:

  1. GOPATH 環境変数が設定されていることを確認します。
  2. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    go get -d github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
  3. サンプルコードが含まれているディレクトリに移動します。
    cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/endpoints/getting-started
PHP

サンプル API のクローンを作成するか、ダウンロードするには:

  1. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    git clone https://github.com/GoogleCloudPlatform/php-docs-samples

    または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。

  2. サンプルコードが含まれているディレクトリに移動します。
    cd php-docs-samples/endpoints/getting-started
Ruby

サンプル API のクローンを作成するか、ダウンロードするには:

  1. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples

    または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。

  2. サンプルコードが含まれているディレクトリに移動します。
    cd ruby-docs-samples/endpoints/getting-started
NodeJS

サンプル API のクローンを作成するか、ダウンロードするには:

  1. ローカルマシンにサンプルアプリのリポジトリのクローンを作成します。
    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples

    または、zip ファイルとしてサンプルをダウンロードし、ファイルを解凍します。

  2. サンプルコードが含まれているディレクトリに移動します。
    cd nodejs-docs-samples/endpoints/getting-started

Endpoints を構成する

OpenAPI 2.0 または OpenAPI 3.x に基づいて、アプリの surface と認証要件を記述する OpenAPI ドキュメントを用意する必要があります。詳細については、サポートされている OpenAPI バージョンをご覧ください。

また、ESPv2 がアプリを呼び出すために必要な情報を取得できるよう、このドキュメントには各アプリの URL を設定した Google 固有のフィールドも追加する必要があります。OpenAPI を初めて使用する場合は、OpenAPI の概要で詳細をご覧ください。

OpenAPI 2.0

OpenAPI 2.0 仕様を使用して Endpoints を構成するには、ダウンロードしたサンプルコードの dotnet-docs-samples-master\endpoints\getting-started ディレクトリにある openapi-appengine.yaml ファイルを使用します。

OpenAPI 2.0 仕様の内容は次のようになります。

swagger: "2.0"
info:
  description: "A simple Google Cloud Endpoints API example."
  title: "Endpoints Example"
  version: "1.0.0"
host: "YOUR_PROJECT_ID.appspot.com"
consumes:
- "application/json"
produces:
- "application/json"
schemes:
- "https"
paths:
  "/echo":
    post:
      description: "Echo back a given message."
      operationId: "echo"
      produces:
      - "application/json"
      responses:
        200:
          description: "Echo"
          schema:
            $ref: "#/definitions/echoMessage"
      parameters:
      - description: "Message to echo"
        in: body
        name: message
        required: true
        schema:
          $ref: "#/definitions/echoMessage"
      security:
      - api_key: []
  "/auth/info/googlejwt":
    get:
      description: "Returns the requests' authentication information."
      operationId: "auth_info_google_jwt"
      produces:
      - "application/json"
      responses:
        200:
          description: "Authentication info."
          schema:
            $ref: "#/definitions/authInfoResponse"
      x-security:
      - google_jwt:
          audiences:
          # This must match the "aud" field in the JWT. You can add multiple
          # audiences to accept JWTs from multiple clients.
          - "echo.endpoints.sample.google.com"
  "/auth/info/googleidtoken":
    get:
      description: "Returns the requests' authentication information."
      operationId: "authInfoGoogleIdToken"
      produces:
      - "application/json"
      responses:
        200:
          description: "Authenication info."
          schema:
            $ref: "#/definitions/authInfoResponse"
      x-security:
      - google_id_token:
          audiences:
          # Your OAuth2 client's Client ID must be added here. You can add
          # multiple client IDs to accept tokens from multiple clients.
          - "YOUR-CLIENT-ID"

definitions:
  echoMessage:
    type: "object"
    properties:
      message:
        type: "string"
  authInfoResponse:
    properties:
      id:
        type: "string"
      email:
        type: "string"

securityDefinitions:
  # This section configures basic authentication with an API key.
  api_key:
    type: "apiKey"
    name: "key"
    in: "query"
  # This section configures authentication using Google API Service Accounts
  # to sign a json web token. This is mostly used for server-to-server
  # communication.
  google_jwt:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    # This must match the 'iss' field in the JWT.
    x-google-issuer: "jwt-client.endpoints.sample.google.com"
    # Update this with your service account's email address.
    x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR-SERVICE-ACCOUNT-EMAIL"
  # This section configures authentication using Google OAuth2 ID Tokens.
  # ID Tokens can be obtained using OAuth2 clients, and can be used to access
  # your API on behalf of a particular user.
  google_id_token:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "https://accounts.google.com"
    x-google-jwks_uri: "https://www.googleapis.com/oauth2/v1/certs"

host フィールドのある行で、YOUR_PROJECT_ID を Google Cloud プロジェクト ID に置き換えます。

OpenAPI 3.x

OpenAPI 3.x 仕様を使用して Endpoints を構成するには、ダウンロードしたサンプルコードの dotnet-docs-samples-master\endpoints\getting-started ディレクトリにある openapi-appengine.yaml ファイルの内容を置き換えます。

  1. テキスト エディタで openapi-appengine.yaml を開き、内容を次のように置き換えます。
    openapi: 3.0.4
    info:
      description: "A simple Google Cloud Endpoints API example."
      title: "Endpoints Example"
      version: "1.0.0"
    servers:
      - url: "https://YOUR_PROJECT_ID.appspot.com"
        x-google-endpoint: {}
    paths:
      "/echo":
        post:
          description: "Echo back a given message."
          operationId: "echo"
          requestBody:
            description: "Message to echo"
            required: true
            content:
              "application/json":
                schema:
                  $ref: "#/components/schemas/echoMessage"
          responses:
            "200":
              description: "Echo"
              content:
                "application/json":
                  schema:
                    $ref: "#/components/schemas/echoMessage"
          security:
            - api_key: []
      "/auth/info/googlejwt":
        get:
          description: "Returns the requests' authentication information."
          operationId: "auth_info_google_jwt"
          responses:
            "200":
              description: "Authenication info."
              content:
                "application/json":
                  schema:
                    $ref: "#/components/schemas/authInfoResponse"
          security:
            - google_jwt: []
      "/auth/info/googleidtoken":
        get:
          description: "Returns the requests' authentication information."
          operationId: "authInfoGoogleIdToken"
          responses:
            "200":
              description: "Authenication info."
              content:
                "application/json":
                  schema:
                    $ref: "#/components/schemas/authInfoResponse"
          security:
            - google_id_token: []
    
    components:
      schemas:
        echoMessage:
          type: "object"
          properties:
            message:
              type: "string"
        authInfoResponse:
          type: "object"
          properties:
            id:
              type: "string"
            email:
              type: "string"
    
      securitySchemes:
        # This section configures basic authentication with an API key.
        api_key:
          type: apiKey
          name: key
          in: query
        # This section configures authentication using Google API Service Accounts
        # to sign a json web token. This is mostly used for server-to-server
        # communication.
        google_jwt:
          type: oauth2
          flows:
            implicit:
              authorizationUrl: ""
              scopes: {}
          x-google-auth:
            issuer: "jwt-client.endpoints.sample.google.com"
            jwksUri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR_SERVICE_ACCOUNT_EMAIL"
            # This must match the "aud" field in the JWT. You can add multiple
            # audiences to accept JWTs from multiple clients.
            audiences:
              - "echo.endpoints.sample.google.com"
        # This section configures authentication using Google OAuth2 ID Tokens.
        # ID Tokens can be obtained using OAuth2 clients, and can be used to access
        # your API on behalf of a particular user.
        google_id_token:
          type: oauth2
          flows:
            implicit:
              authorizationUrl: ""
              scopes: {}
          x-google-auth:
            issuer: "https://accounts.google.com"
            jwksUri: "https://www.googleapis.com/oauth2/v1/certs"
            audiences:
              - "YOUR_CLIENT_ID"
  2. openapi.yaml の新しい内容を保存します。

このチュートリアルでは、サービス名を構成できる OpenAPI 仕様の Google 固有の拡張機能を使用します。サービス名を指定する方法は、使用している OpenAPI 仕様のバージョンによって異なります。

OpenAPI 2.0

host フィールドを使用して、サービス名を指定します。

host: YOUR_PROJECT_ID.appspot.com

Endpoints を構成するには:

  1. openapi-appengine.yaml ファイルを開きます。
  2. host フィールドで、YOUR_PROJECT_ID を Google Cloud プロジェクト ID に置き換えます。
  3. openapi-appengine.yaml ファイルを保存します。

OpenAPI 3.x

サービス名を指定するには、servers オブジェクトの url フィールドを使用します。

servers:
- url: https://YOUR_PROJECT_ID.appspot.com
  x-google-endpoint: {}

Endpoints を構成するには:

  1. openapi-appengine.yaml ファイルを開きます。
  2. openapi-appengine.yaml ファイルに host フィールドがある場合は、削除します。
  3. 次に示すように、servers オブジェクトを追加します。
  4. url フィールドで、YOUR_PROJECT_ID を Google Cloud プロジェクト ID に置き換えます。
  5. openapi-appengine.yaml ファイルを保存します。

Endpoints 構成をデプロイする

Endpoints の構成をデプロイするには、gcloud endpoints services deploy コマンドを使用します。このコマンドを実行すると、Service Management を使用してマネージド サービスが作成されます。

Endpoints 構成をデプロイするには:

  1. openapi.yaml 構成ファイルがあるディレクトリにいることを確認します。
  2. 構成をアップロードしてマネージド サービスを作成します。
    gcloud endpoints services deploy openapi.yaml
    

gcloud コマンドが Service Management API を呼び出して、openapi.yaml ファイルの host フィールドまたは servers.url フィールドで指定した名前のマネージド サービスを作成します。Service Management は、openapi.yaml ファイル内の設定に従ってサービスを構成します。openapi.yaml に変更を加えるときは、このファイルを再デプロイして Endpoints サービスを更新する必要があります。

Service Management でサービスの作成と構成が行われるとき、情報がターミナルに出力されます。openapi.yaml ファイル内のパスが API キーを要求していないことを示す警告は無視して問題ありません。サービスの構成が完了すると、Service Management に、次のようなサービス構成 ID とサービス名を含むメッセージが表示されます。

Service Configuration [2017-02-13r0] uploaded for service [example-project-12345.appspot.com]

上記の例では、2017-02-13r0 はサービス構成 ID、example-project-12345.appspot.com は Endpoints サービスです。サービス構成 ID は、日付スタンプとそれに続くリビジョン番号で構成されます。同じ日に openapi.yaml ファイルを再度デプロイすると、サービス構成 ID のリビジョン番号が増分されます。Endpoints サービスの構成は、 Google Cloud コンソールの [エンドポイント] > [サービス] ページで確認できます。

エラー メッセージが表示された場合は、Endpoints 構成のデプロイのトラブルシューティングをご覧ください。

必要なサービスの確認

Endpoints と ESP を使用するには、少なくとも次の Google サービスの有効化が必要です。
名前 タイトル
servicemanagement.googleapis.com Service Management API
servicecontrol.googleapis.com Service Control API

ほとんどの場合、gcloud endpoints services deploy コマンドによってこれらの必須サービスが有効化されます。ただし、以下の状況では、gcloud コマンドは正常に完了しますが、必須サービスが有効になりません。

  • Terraform などのサードパーティのアプリケーションを使用していて、上記のサービスを含めていない場合。

  • 上記のサービスが明示的に無効にされている既存のGoogle Cloud プロジェクトに Endpoints 構成をデプロイした場合。

必要なサービスが有効になっていることを確認するには、次のコマンドを実行します。

gcloud services list

必要なサービスが表示されない場合は、次のコマンドを使用してサービスを有効にします。

gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com

Endpoints サービスも有効にします。

gcloud services enable ENDPOINTS_SERVICE_NAME

ENDPOINTS_SERVICE_NAME を確認するには、次のいずれかを行います。

  • Endpoints 構成をデプロイ後、Cloud コンソールの [Endpoints] ページに移動します。[サービス名] 列に、考えられる ENDPOINTS_SERVICE_NAME のリストが表示されます。

  • OpenAPI の場合、ENDPOINTS_SERVICE_NAME は OpenAPI 仕様の host フィールドで指定したものです。gRPC の場合、ENDPOINTS_SERVICE_NAME は gRPC Endpoints 構成の name フィールドで指定したものです。

gcloud コマンドの詳細については、gcloud サービスをご覧ください。

API バックエンドをデプロイする

ここまでの手順で OpenAPI ドキュメントを Service Management にデプロイしましたが、API バックエンドを処理するコードはまだデプロイしていません。ここでは、サンプル API と ESP を App Engine にデプロイします。

API バックエンドをデプロイするには:

  1. サービス名を app.yaml ファイルに追加します。

    Java

    endpoints/getting-started/src/main/appengine/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Python

    endpoints/getting-started/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Go

    endpoints/getting-started/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    PHP

    endpoints/getting-started/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command. If you have
      # previously run the deploy command, you can list your existing configuration
      # ids using the 'configs list' command as follows:
      #
      #     gcloud endpoints configs list --service=YOUR-PROJECT-ID.appspot.com
      #
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    Ruby

    endpoints/getting-started/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    NodeJS

    endpoints/getting-started/app.yaml ファイルを開きます。

    endpoints_api_service:
      # The following values are to be replaced by information from the output of
      # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
      name: ENDPOINTS-SERVICE-NAME
      rollout_strategy: managed

    ENDPOINTS-SERVICE-NAME を Endpoints サービスの名前で置き換えます。これは、OpenAPI ドキュメントの host フィールドで構成した名前と同じです。次に例を示します。

    endpoints_api_service:
      name: example-project-12345.appspot.com
      rollout_strategy: managed

    rollout_strategy: managed オプションを指定すると、デプロイ済みの最新のサービス構成を使用するように ESP が構成されます。このオプションを指定すると、新しいサービス構成をデプロイしてから 5 分以内に ESP が変更を検出し、自動的に使用します。ESP が特定の構成 ID でなく、このオプションを使用するようにしてください。

  2. app.yaml ファイルを保存します。
  3. endpoints_api_service セクションが app.yaml ファイルに含まれているため、gcloud app deploy コマンドは ESP を App Engine フレキシブル環境の別のコンテナにデプロイして構成します。すべてのリクエスト トラフィックは ESP 経由でルーティングされ、ESP はバックエンド サーバーコードを実行するコンテナとの間でリクエストとレスポンスの送受信を行います。

  4. openapi.yaml 構成ファイルがあるディレクトリにいることを確認します。
  5. 次のコマンドを実行して、サンプル API と ESP を App Engine にデプロイします。
  6. Java

    Maven を使用してデプロイします。

    mvn appengine:stage
    gcloud app deploy target/appengine-staging
    
    Python
    gcloud app deploy
    Go
    gcloud app deploy
    PHP
    gcloud app deploy
    Ruby
    gcloud app deploy
    NodeJS
    gcloud app deploy

    gcloud app deploy コマンドは、YOUR_PROJECT_ID.appspot.com という形式の DNS レコードを作成しますが、これは API にリクエストを送信するときに使用します。App Engine が完全に初期化される間、API にリクエストを送信するまで数分待つことをおすすめします。

エラー メッセージが表示された場合は、App Engine フレキシブル環境でのデプロイのトラブルシューティングをご覧ください。

詳しくは、API バックエンドをデプロイするをご覧ください。

API にリクエストを送信する

サービスが App Engine 上で実行されていますので、それにリクエストを送信できます。

API キーを作成し、環境変数を設定する

サンプルコードには API キーが必要です。リクエストを簡単にするために、API キーの環境変数を設定します。

  1. API に使用したのと同じ Google Cloud プロジェクトの API 認証情報ページで API キーを作成します。別の Google Cloud プロジェクトで API キーを作成する場合は、 Google Cloud プロジェクトで API を有効にするをご覧ください。

    [認証情報] ページに移動

  2. [認証情報を作成] をクリックして [API キー] を選択します。
  3. キーをクリップボードにコピーします。
  4. [閉じる] をクリックします。
  5. ローカルマシンで、API キーを貼り付けて環境変数に割り当てます。
    • Linux または Mac OS の場合: export ENDPOINTS_KEY=AIza...
    • Windows PowerShell の場合: $Env:ENDPOINTS_KEY="AIza..."

リクエストを送信する

Linux または Mac OS

  1. App Engine プロジェクト URL の環境変数を作成します。YOUR_PROJECT_ID は、実際のGoogle Cloud プロジェクト ID に置き換えます。

    export ENDPOINTS_HOST=https://YOUR_PROJECT_ID.appspot.com

  2. 設定済みの ENDPOINTS_HOST 環境変数と ENDPOINTS_KEY 環境変数を使用して、HTTP リクエストを送信します。

    curl --request POST \
      --header "content-type:application/json" \
      --data '{"message":"hello world"}' \
      "${ENDPOINTS_HOST}/echo?key=${ENDPOINTS_KEY}"

上記の curl で、

  • --data オプションは、API に送信するデータを指定します。
  • --header オプションは、データが JSON 形式であることを指定します。

PowerShell

  1. App Engine プロジェクト URL の環境変数を作成します。YOUR_PROJECT_ID は、実際のGoogle Cloud プロジェクト ID に置き換えます。

    $Env:ENDPOINTS_HOST="https://YOUR_PROJECT_ID.appspot.com"

  2. 設定済みの ENDPOINTS_HOST 環境変数と ENDPOINTS_KEY 環境変数を使用して、HTTP リクエストを送信します。

    (Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' `
        -Headers @{"content-type"="application/json"} `
        -URI "$Env:ENDPOINTS_HOST/echo?key=$Env:ENDPOINTS_KEY").Content
    

上記の例では、最初の 2 行はバッククォートで終わります。この例を PowerShell に貼り付けるとき、バッククォートの後にスペースがないことを確認してください。このリクエスト例で使用されているオプションについては、Microsoft のドキュメントの Invoke-WebRequest をご覧ください。

サードパーティ製アプリ

Chrome ブラウザの拡張機能である Postman などのサードパーティのアプリケーションを使用してリクエストを送信できます。

  • HTTP 動詞として POST を選択します。
  • ヘッダーで、キー content-type とその値 application/json を選択します。
  • 本文で、次のように入力します。
    {"message":"hello world"}
  • URL では、環境変数ではなく実際の appspot.com アドレスと API キーを使用します。次に例を示します。
    https://example-project-12345.appspot.com/echo?key=AIza...

API によって送信メッセージがエコーバックされ、次のようなレスポンスが返されます。

{
  "message": "hello world"
}

正常なレスポンスが返されなかった場合は、レスポンス エラーのトラブルシューティングをご覧ください。

これで Endpoints の API のデプロイとテストが完了しました。

API の活動を追跡する

  1. [エンドポイント] ページで API のアクティビティ グラフを確認します。

    [Endpoints] の [サービス] ページに移動

    リクエストがグラフに反映されるまでにしばらく時間がかかることがあります。

  2. [ログ エクスプローラ] ページで API のリクエストログを確認します。

    [ログ エクスプローラ] ページに移動

クリーンアップ

このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。

このチュートリアルで使用したサービスを停止するには、API と API インスタンスを削除するをご覧ください。

次のステップ