TypeScript 샘플
다음 TypeScript 코드 샘플은 IBM Cloud® IAM 토큰을 얻고 이를 서비스 ID 토큰으로 교환하는 시스템 사용자 인증 프로세스를 프로그래밍 방식으로 관리합니다. 이 코드를 사용하여 Transparent Supply에 대한 데이터 업로드 자동화를 시작할 수 있습니다.
TypeScript 코드
const request = require('request-promise');
const errors = require('request-promise/errors');
export async function getToken(base_url: string,
organization_id: string,
apikey: string): Promise<string> {
const iamTokenData = await getCloudIamToken(apikey);
return await exchangeToken(base_url, organization_id , iamTokenData);
}
function getCloudIamToken(apikey: string): Promise<CloudIamToken> {
const parameters = {
grant_type : 'urn:ibm:params:oauth:grant-type:apikey',
apikey : apikey
};
const requestOptions = {
uri: 'https://iam.cloud.ibm.com/identity/token',
method: 'POST',
headers: {
'Accept': 'application/json'
},
form: parameters, // adds header 'Content-Type' = 'application/x-www-form-urlencoded'
json: true
};
return request(requestOptions)
.then((resp: CloudIamToken) => {
return resp;
})
.catch(errors.StatusCodeError, (rpError: any) => {
Promise.reject(rpError.message);
})
.catch(errors.RequestError, (rpError: any) => {
Promise.reject(rpError.error);
});
}
function exchangeToken(base_url: string,
organization_id: string,
iamTokenData: CloudIamToken): Promise<string> {
const endpoint_url = base_url + '/ift/api/identity-proxy/exchange_token/v1/organization/' + organization_id;
const requestOptions = {
uri: endpoint_url,
method: 'POST',
headers: {
'Accept': 'application/json'
},
body: iamTokenData,
json: true
};
return request(requestOptions)
.then((resp: ExchangeTokenResponse) => {
return resp.onboarding_token;
})
.catch(errors.StatusCodeError, (rpError: any) => {
Promise.reject(rpError.message);
})
.catch(errors.RequestError, (rpError: any) => {
Promise.reject(rpError.error);
});
}
interface CloudIamToken {
access_token : string;
token_type : string;
expires_in : number;
expiration : number;
}
interface ExchangeTokenResponse {
onboarding_token: string;
}