React Native
React Native
AWS Security Token Service (STS) GetSessionToken
See more AWS Security Token Service Examples
Returns a set of temporary credentials for an AWS account or IAM user.Chilkat React Native Downloads
import { AuthAws, Rest, Xml } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = new Rest();
// Connect to the Amazon AWS REST server.
// such as https://sts.us-west-2.amazonaws.com/
const bTls = true;
const port = 443;
const bAutoReconnect = true;
await rest.connectAsync('sts.us-west-2.amazonaws.com', port, bTls, bAutoReconnect);
// Provide AWS credentials for the REST call.
const authAws = new AuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
// the region should match our URL above..
// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html
authAws.region = 'us-west-2';
authAws.serviceName = 'sts';
rest.setAuthAws(authAws);
rest.addQueryParam('Version', '2011-06-15');
rest.addQueryParam('Action', 'GetSessionToken');
rest.addQueryParam('DurationSeconds', '3600');
let responseXml: string;
try {
responseXml = await rest.fullRequestNoBodyAsync('GET', '/');
} catch {
console.log(rest.lastErrorText);
return;
}
// A successful response will have a status code equal to 200.
if (rest.responseStatusCode !== 200) {
console.log(`response status code = ${rest.responseStatusCode}`);
console.log(`response status text = ${rest.responseStatusText}`);
console.log(`response header: ${rest.responseHeader}`);
console.log(`response body: ${responseXml}`);
return;
}
// Examine the successful XML response (shown below)
const xml = new Xml();
xml.loadXml(responseXml);
console.log(xml.getXml());
// Sample response:
// <?xml version="1.0" encoding="utf-8"?>
// <GetSessionTokenResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
// <GetSessionTokenResult>
// <Credentials>
// <AccessKeyId>AS........T4N</AccessKeyId>
// <SecretAccessKey>05W........ARPMr</SecretAccessKey>
// <SessionToken>IQoJb3J........llpIMI=</SessionToken>
// <Expiration>2022-09-07T00:22:51Z</Expiration>
// </Credentials>
// </GetSessionTokenResult>
// <ResponseMetadata>
// <RequestId>8bad22cc-1c55-4265-a010-45d139359404</RequestId>
// </ResponseMetadata>
// </GetSessionTokenResponse>
// Sample parse code:
const getSessionTokenResponseXmlns = xml.getAttrValue('xmlns');
const accessKeyId = xml.getChildContent('GetSessionTokenResult|Credentials|AccessKeyId');
const secretAccessKey = xml.getChildContent('GetSessionTokenResult|Credentials|SecretAccessKey');
const sessionToken = xml.getChildContent('GetSessionTokenResult|Credentials|SessionToken');
const expiration = xml.getChildContent('GetSessionTokenResult|Credentials|Expiration');
const requestId = xml.getChildContent('ResponseMetadata|RequestId');
// Save the session token XML to a file for use by another Chilkat example..
xml.saveXml('qa_data/tokens/aws_session_token.xml');
}