4.2. REST API를 사용한 인증


API 호출에는 API 토큰을 사용한 인증이 필요합니다. API_TOKEN 을 변수 이름으로 사용하는 경우 REST API로 인증하기 위해 API 호출에 -H "Authorization: Bearer ${API_TOKEN}" 를 추가합니다.

참고

API 토큰은 15분 후에 만료됩니다.

사전 요구 사항

  • (선택 사항) OpenShift Cluster Manager (ocm) CLI 도구를 설치했습니다.

절차

  1. OFFLINE_TOKEN 을 사용하여 API_TOKEN 변수를 설정하여 사용자를 검증합니다.

    1. (선택 사항) 명령줄 터미널에서 다음 명령을 실행합니다.

      $ export API_TOKEN=$( \
        curl \
        --silent \
        --header "Accept: application/json" \
        --header "Content-Type: application/x-www-form-urlencoded" \
        --data-urlencode "grant_type=refresh_token" \
        --data-urlencode "client_id=cloud-services" \
        --data-urlencode "refresh_token=${OFFLINE_TOKEN}" \
        "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" \
        | jq --raw-output ".access_token" \
      )
    2. (선택 사항) 명령줄 터미널에서 ocm 클라이언트에 로그인합니다.

      $ ocm login --token="${OFFLINE_TOKEN}"

      그런 다음 API 토큰을 생성합니다.

      $ export API_TOKEN=$(ocm token)
  1. 토큰 생성 방법 중 하나의 경로에 스크립트를 생성합니다. 예를 들면 다음과 같습니다.

    $ vim ~/.local/bin/refresh-token
    export API_TOKEN=$( \
      curl \
      --silent \
      --header "Accept: application/json" \
      --header "Content-Type: application/x-www-form-urlencoded" \
      --data-urlencode "grant_type=refresh_token" \
      --data-urlencode "client_id=cloud-services" \
      --data-urlencode "refresh_token=${OFFLINE_TOKEN}" \
      "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" \
      | jq --raw-output ".access_token" \
    )

    그런 다음 파일을 저장합니다.

  2. 파일 모드를 변경하여 실행 가능하게 합니다.

    $ chmod +x ~/.local/bin/refresh-token
  3. API 토큰을 새로 고칩니다.

    $ source refresh-token
  4. 다음 명령을 실행하여 API에 액세스할 수 있는지 확인합니다.

    $ curl -s https://api.openshift.com/api/assisted-install/v2/component-versions -H "Authorization: Bearer ${API_TOKEN}" | jq

    출력 예

    {
      "release_tag": "v2.11.3",
      "versions": {
        "assisted-installer": "registry.redhat.io/rhai-tech-preview/assisted-installer-rhel8:v1.0.0-211",
        "assisted-installer-controller": "registry.redhat.io/rhai-tech-preview/assisted-installer-reporter-rhel8:v1.0.0-266",
        "assisted-installer-service": "quay.io/app-sre/assisted-service:78d113a",
        "discovery-agent": "registry.redhat.io/rhai-tech-preview/assisted-installer-agent-rhel8:v1.0.0-195"
      }
    }

Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.