-
Notifications
You must be signed in to change notification settings - Fork 34
How To Get an Authentication Token for System Users TypeScript Code Sample
const request = require('request-promise');
const errors = require('request-promise/errors');
export async function getToken(mmo_host: string,
mmo_instance_id: string,
mmo_solution_id: string,
mmo_organization_id: string,
apikey: string): Promise<string> {
const iamTokenData = await getBluemixIamToken(apikey);
return await exchangeToken(mmo_host, mmo_instance_id, mmo_solution_id , mmo_organization_id , iamTokenData);
}
export function getBluemixIamToken(apikey: string): Promise<BluemixIamToken> {
const parameters = {
grant_type : 'urn:ibm:params:oauth:grant-type:apikey',
apikey : apikey
};
const requestOptions = {
uri: 'https://iam.ng.bluemix.net/oidc/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: BluemixIamToken) => {
return resp;
})
.catch(errors.StatusCodeError, (rpError: any) => {
Promise.reject(rpError.message);
})
.catch(errors.RequestError, (rpError: any) => {
Promise.reject(rpError.error);
});
}
export function exchangeToken(mmo_host: string,
mmo_instance_id: string,
mmo_solution_id: string,
mmo_organization_id: string,
iamTokenData: BluemixIamToken): Promise<string> {
const mmo_endpoint_url = 'https://' + mmo_host + '/' + mmo_instance_id + '/onboarding' +
'/v1/iam/exchange_token' +
'/solution/' + mmo_solution_id +
'/organization/' + mmo_organization_id;
const requestOptions = {
uri: mmo_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);
});
}
export interface BluemixIamToken {
access_token : string;
token_type : string;
expires_in : number;
expiration : number;
}
export interface ExchangeTokenResponse {
onboarding_token: string;
}
WELCOME!
Modules
Membership
Languages
Browsers
ONBOARDING
Onboarding Steps
Data Requirements
Data Types
Supplier Data
Payload Data
Insights Data
HOW-TO
Join by Invitation
Log in as New User
Authenticate Human Users
Authenticate System Users
Java Sample
Typescript Sample
IIB Sample
Assign User Roles
Upload Data
Automate Data Upload
Convert Spreadsheets
Convert Data
Whitelist Custom URLs
APIs-Swagger
Connector API
Documents API
Converter API
Trace API
Insights API
APIs-Usage
APIs
Insights API
Insights API Usage
Trace API
Connector API Errors
API Error Codes
REFERENCE
GS1
GS1 Identifier Reuse
Authentication
Identifiers
Message Codes
Cryptographic
Signatures
Signature Header
Access Control
Firewall Settings
XML to JSON
EPCIS Aggregation Add
EPCIS Aggregation Delete
EPCIS Object Add
EPCIS Object Delete
EPCIS Object Observed
EPCIS Transformation
Purchase Order
Despatch Advice
Receiving Advice
Master Data Item
Master Data Facility
Standard Business
Document Header