IBM Cloud Docs
IBM Cloud 서비스 API 호출

IBM Cloud 서비스 API 호출

애플리케이션과 서비스가 API 호출을 할 수 있도록 권한을 부여하려면, 서비스의 API에 사용자 신원을 인증하고 서비스의 맥락 내에서 작업을 수행할 수 있는 액세스 권한을 부여하기 위해 자격 증명을 전달해야 합니다.

다음 방법 중 하나로 호출자를 식별할 수 있습니다.

  • IBM Cloud IAM(Identity and Access Management) 토큰
  • IBM Cloud API 키 또는 서비스 ID API 키

IBM Cloud API 키, 서비스 ID API 키 및 IAM 토큰이 호출자의 ID를 고유하게 식별합니다. 호출자 ID는 IBM Cloud 계정에서 작성된 IBM Cloud 사용자 또는 서비스 ID입니다.

API 키는 긴 일련의 임의 문자 또는 숫자로 구성된 인증 정보입니다. IBM Cloud ID에 여러 API 키가 포함될 수 있습니다. 이러한 각 API 키는 개별적으로 관리될 수 있으며, 이는 이 API 키가 서비스에서만 사용되는 경우 다른 컴포넌트를 방해하지 않고 API 키를 삭제할 수 있음을 의미합니다.

API 키를 사용하여 IBM Cloud 명령행 인터페이스(CLI)에 로그인하거나 IAM 토큰을 생성할 수 있습니다. 이 API 키가 프로덕션 용도로 권장되지 않는 경우 API 키를 IBM Cloud 서비스로 보낼 수 있습니다.

IBM Cloud IAM 토큰을 전달하여 서비스 API에서 인증

IAM 액세스 토큰을 검색하려면 API 클라이언트가 먼저 IBM Cloud IAM API를 호출하여 이 토큰을 인증하고 검색해야 합니다. IBM Cloud 서비스 API 클라이언트에 대한 선호되는 방식은 IBM Cloud API 키를 사용하여 IAM 액세스 토큰을 가져오는 것입니다. JSON 웹 토큰으로 구현된 IAM 액세스 토큰은 IAM 액세스 토큰을 인증 방법으로 받아들이는 IBM Cloud 서비스의 여러 호출에 사용할 수 있습니다. IAM 액세스 토큰이 비대칭 키를 사용하여 디지털로 서명되었으므로 IBM Cloud 서비스는 외부 서비스를 호출하지 않고 IAM 액세스 토큰을 유효성 검증할 수 있습니다. 이는 API 호출 성능을 크게 향상시킵니다.

액세스 토큰을 사용하여 서비스 API로 인증
API 키를 사용하여 IAM에서 토큰을 검색하고 액세스 토큰을 대상 서비스에 전달하여 자격 증명을 확인
'

액세스 토큰을 사용하여 서비스 API에서 인증하려면 다음 단계를 완료하십시오.

  1. 아직 없는 경우 먼저 IBM Cloud API 키를 작성하십시오.
  2. API 키에서 IAM 토큰 가져오기에 설명된 대로 API 클라이언트에 대한 다음 단계는 IAM 액세스 토큰 검색입니다.
  3. 응답에서 access_token 특성을 추출하여 IAM 액세스 토큰을 가져옵니다. expires_in은(는) IAM 액세스 토큰 access_token이(가) 만료될 때까지의 초를 나타냅니다. 이 상대 값 또는 UNIX 시간 기반의 절대 타임스탬프 expiration 를 사용하십시오.
  4. IAM 액세스 토큰을 다음과 같이 전송합니다 RFC 6750, 섹션 2.1. 승인 요청 헤더 필드.

다음 예를 검토하십시오.

  1. HTTP 헤더 권한 사용
  2. 접두사 IAM 액세스 토큰에 리터럴 Bearer eyJhbGciOiJSUzI1Ng...
  3. HTTP 헤더에 접두사 IAM 액세스 토큰을 추가합니다. Authorization: Bearer eyJhbGciOiJSUzI1Ng...
curl -H "Authorization: Bearer eyJhbGciOiJSUzI1Ng..."
import com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator;
import <sdk_base_package>.ExampleService.v1.ExampleService;
...
String bearerToken = // ... obtain bearer token value ...

// Create the authenticator.
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(bearerToken);

// Create the service instance.
ExampleService service = new ExampleService(authenticator);

// 'service' can now be used to invoke operations.
...
// Later, if your bearer token value expires, you can set a new one like this:
newToken = // ... obtain new bearer token value
authenticator.setBearerToken(newToken);
const ExampleServiceV1 = require('mysdk/example-service/v1');
const { BearerTokenAuthenticator } = require('mysdk/auth');

const authenticator = new BearerTokenAuthenticator({
  bearerToken: '<access-token>',
});

const myService = new ExampleServiceV1({
  authenticator,
});
...

// Later when the access token expires, the application must acquire
// a new access token, then set it on the authenticator.
// Subsequent request invocations will include the new access token.
authenticator.setBearerToken('<new-access-token>')
from ibm_cloud_sdk_core.authenticators import BearerTokenAuthenticator

authenticator = BearerTokenAuthenticator(<your_bearer_token>)
service = ExampleService(authenticator=authenticator)

# after getting a new access token...
service.get_authenticator().set_bearer_token('54321');
    import {
    "github.com/IBM/go-sdk-core/v5/core"
    "<appropriate-git-repo-url>/exampleservicev1"
}
...
// Create the authenticator.
bearerToken := // ... obtain bearer token value ...
authenticator := &core.BearerTokenAuthenticator{
    BearerToken: bearerToken,
}

// Create the service options struct.
options := &exampleservicev1.ExampleServiceV1Options{
    Authenticator: authenticator,
}

// Construct the service instance.
service := exampleservicev1.NewExampleServiceV1(options)

// 'service' can now be used to invoke operations.
...
// Later, if your bearer token value expires, you can set a new one like this:
newToken := // ... obtain new bearer token value
authenticator.BearerToken = newToken

후속 IBM Cloud 서비스 API 호출에 동일한 IAM 액세스 토큰을 사용하여 최고의 성능과 확장성을 달성하십시오.

Java SDK 참조

노드 SDK 참조

Python SDK 참조

Go SDK 참조

IBM Cloud API 키를 전달하여 서비스 API에서 인증

API 클라이언트는 IBM Cloud API 키를 직접 대상 서비스의 API에 전달할 수 있습니다. 이렇게 하려면 기본 권한 HTTP 헤더를 사용하여 apikey 키워드를 사용자 이름으로, IBM Cloud API 키를 비밀번호로 대상 서비스에 보내십시오.

API 키를 서비스 API에 전달하는 모든 경우에 특정 서비스에 대해 작업하는 데 필요한 액세스 레벨만 지정된 기능 ID와 연관된 API 키를 서비스 ID 또는 사용자 API 키로 사용하는 것이 좋습니다.

대상 서비스 API는 IBM Cloud IAM 서비스를 사용하여 IBM Cloud API 키를 조사해야 합니다. 다음 그래픽은 세 가지 API 상호작용을 보여줍니다. IBM Cloud API 키는 모든 대상 서비스의 API로 전달되므로, 각 대상 서비스가 IBM Cloud IAM을 호출하여 IBM Cloud API 키 세부사항을 검색해야 합니다.

API 키를 사용하여 서비스 API로 인증
API 키를 대상 서비스에 전달하면 대상 서비스가 API 키를 IAM에 전달하여 자격 증명을 검증

IBM Cloud API 키 사용은 편리하며 이를 통해 새 API를 쉽게 검색하고 신속하게 프로토타입을 사용해 볼 수 있습니다. 이 방법에서는 IBM Cloud API 키를 읽기 가능한 형식으로 대상 서비스의 API에 보내야 하며, 이는 불필요하게 API 키를 손상시킵니다. 또한 대상 서비스의 API는 항상 API 키를 조사해야 하기 때문에 이 방법은 성능을 저하시키므로 프로덕션 워크로드에 권장되지 않습니다.

API 키를 사용하여 서비스의 API에서 인증하려면 다음 단계를 완료하십시오.

  1. 아직 없는 경우 먼저 IBM Cloud API 키를 작성하십시오.
  2. RFC 7617 에 정의된 대로 IBM Cloud API 키를 HTTP 헤더의 "Authorization"에 입력합니다. apikey를 사용자 이름으로, API 키 값을 비밀번호로 사용하십시오.

예로서 다음 단계에서는 API 키가 0a1A2b3B4c5C6d7D8e9E라고 가정합니다.

  1. 사용자 이름 apikey와 콜론으로 구분된 API 키를 연결하십시오. apikey:0a1A2b3B4c5C6d7D8e9E
  2. Base64가 문자열 인코딩: base64("apikey:0a1A2b3B4c5C6d7D8e9E") => YXBpa2V5OjBhMUEyYjNCNGM1QzZkN0Q4ZTlF
  3. 스키마가 Basic인 HTTP 헤더 권한을 설정하십시오(예: Authorization: Basic YXBpa2V5OjBhMUEyYjNCNGM1QzZkN0Q4ZTlF). curl 명령을 사용하는 경우 매개변수 -u와 함께 전달할 수 있습니다.
curl -u "apikey:<IBM Cloud API key value>"
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
import <sdk_base_package>.ExampleService.v1.ExampleService;
...
// Create the authenticator.
BasicAuthenticator authenticator = new BasicAuthenticator.Builder()
    .username("myuser")
    .password("mypassword")
    .build();

// Create the service instance.
ExampleService service = new ExampleService(authenticator);

// 'service' can now be used to invoke operations.

from ibm_cloud_sdk_core.authenticators import BasicAuthenticator

authenticator = BasicAuthenticator(<your_username>, <your_password>)
service = ExampleService(authenticator=authenticator)
import {
    "github.com/IBM/go-sdk-core/v5/core"
    "<appropriate-git-repo-url>/exampleservicev1"
}
...
// Create the authenticator.
authenticator := &core.BasicAuthenticator{
    Username: "myuser",
    Password: "mypassword",
}

// Create the service options struct.
options := &exampleservicev1.ExampleServiceV1Options{
    Authenticator: authenticator,
}

// Construct the service instance.
service := exampleservicev1.NewExampleServiceV1(options)

// 'service' can now be used to invoke operations.

사용자 이름은 apikey이고 비밀번호는 API 키 그대로 사용하면 됩니다.