API 키 사용

이 페이지에서는 API 게이트웨이에서 API 키를 사용하는 방법을 설명합니다.

개요

API 키는 할당량, 청구, 모니터링 목적으로Google Cloud 프로젝트를 식별하는 문자열입니다. 개발자는 Google Cloud 콘솔의 프로젝트에서 API 키를 생성합니다. 그런 다음 API에 대한 모든 호출에 이 키를 쿼리 매개변수로 또는 요청 헤더에 포함합니다.

API 구성에 API 키 요구사항을 지정하면 API 게이트웨이가 이 API 키를 사용해서 연결된 Google Cloud 프로젝트를 조회합니다. API 키가 Google Cloud 프로젝트에서 또는 API가 사용 설정된 다른Google Cloud 프로젝트 내에서 생성되지 않았으면 API 게이트웨이가 요청을 거부합니다.

API 키 만들기

API 키를 만들거나 Google Cloud 프로젝트 내에서 이미 제공된 API 키를 보려면 API 및 서비스 > 사용자 인증 정보 페이지로 이동하여 API 키 만들기에 설명된 단계를 완료합니다.

사용자 인증 정보로 이동

API 게이트웨이의 API 키 인증 구성

다음 섹션에 설명된 대로 API 키를 사용하여 게이트웨이에 대한 액세스를 보호하도록 API 게이트웨이의 API 키 인증을 구성합니다.

  1. 서비스에 대해 API 키 지원을 사용 설정합니다.

    Google Cloud 콘솔

    다음 단계를 따르세요.

    1. Google Cloud 콘솔에서 API 및 서비스 > 라이브러리로 이동합니다.

      API 및 서비스 라이브러리로 이동

    2. 검색창에 API의 관리형 서비스 이름을 입력합니다. 이 값은 API 방문 페이지에 있는 API의 관리형 서비스 열에서 찾을 수 있습니다. 예를 들면 다음과 같습니다.
      my-api-123abc456def1.apigateway.my-project.cloud.goog
    3. 서비스 카드를 클릭하여 방문 페이지를 확인합니다.
    4. 서비스의 방문 페이지에서 사용 설정을 클릭합니다.

    Google Cloud CLI

    다음 명령어를 입력합니다. 여기서 MANAGED_SERVICE_NAME은 API를 배포할 때 만든 관리형 서비스의 이름을 지정합니다. gcloud api-gateway apis describe 명령어를 사용해서 나열된 관리형 서비스 속성에서 확인할 수 있습니다.

    gcloud services enable MANAGED_SERVICE_NAME

    예를 들면 다음과 같습니다.

    gcloud services enable my-api-123abc456def1.apigateway.my-project.cloud.goog
  2. 모든 트래픽에 대해 API 키 검증 보안 정책을 적용하도록 API 구성을 만드는 데 사용된 OpenAPI 사양을 수정합니다. 다음과 같이 security 유형과 securityDefinitions 또는 securitySchemes를 추가합니다.

    OpenAPI 2.0

      # openapi2-functions.yaml
      swagger: '2.0'
      info:
        title: API_ID optional-string
        description: Sample API on API Gateway with a Google Cloud Functions backend
        version: 1.0.0
      schemes:
        - https
      produces:
        - application/json
      paths:
        /hello:
          get:
            summary: Greet a user
            operationId: hello
            x-google-backend:
              address: https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGET
            security:
            - api_key: []
            responses:
              '200':
                description: A successful response
                schema:
                  type: string
      securityDefinitions:
        # This section configures basic authentication with an API key.
        api_key:
          type: "apiKey"
          name: "key"
          in: "query"

    securityDefinition은 사양에 정의된 모든 경로에 액세스를 요청할 때 key라는 쿼리 매개변수로 API 키가 전달되도록 API를 구성합니다.

    OpenAPI 3.x

    # openapi-functions.yaml
    openapi: 3.0.4
    info:
      title: API_ID optional-string
      description: Sample API on API Gateway with a Google Cloud Functions backend
      version: 1.0.0
    # Define reusable components in x-google-api-management
    x-google-api-management:
      backends:
        functions_backend:
          address: https://GATEWAY_LOCATION-PROJECT_ID.cloudfunctions.net/helloGET
          pathTranslation: APPEND_PATH_TO_ADDRESS
          protocol: "http/1.1"
    # Apply the backend configuration by referencing it by name. Set at the root so this applies to all operations unless overridden.
    x-google-backend: functions_backend
    components:
    # This section configures basic authentication with an API key.
      securitySchemes:
        google_api_key:
          type: apiKey
          name: x-api-key
          in: header
    security:
      - google_api_key: []
    paths:
      /hello:
        get:
          summary: Greet a user
          operationId: hello
          responses:
            '200':
              description: A successful response
              content:
                application/json:
                  schema:
                    type: string

    securitySchemes은 사양에 정의된 모든 경로에 액세스를 요청할 때 key라는 이름의 쿼리 매개변수로 API 키가 전달되도록 API를 구성합니다.

  3. 다음 명령어를 사용하여 수정된 OpenAPI 설명으로 새 API 구성을 만듭니다.
    gcloud api-gateway api-configs create NEW_CONFIG_ID \
    --api=