Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkXmlW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkAuthAwsW authAws;
const wchar_t *responseXml;
HCkXmlW xml;
const wchar_t *GetSessionTokenResponse_xmlns;
const wchar_t *AccessKeyId;
const wchar_t *SecretAccessKey;
const wchar_t *SessionToken;
const wchar_t *Expiration;
const wchar_t *RequestId;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRestW_Create();
// Connect to the Amazon AWS REST server.
// such as https://sts.us-west-2.amazonaws.com/
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"sts.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
// Provide AWS credentials for the REST call.
authAws = CkAuthAwsW_Create();
CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
CkAuthAwsW_putSecretKey(authAws,L"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
CkAuthAwsW_putRegion(authAws,L"us-west-2");
CkAuthAwsW_putServiceName(authAws,L"sts");
CkRestW_SetAuthAws(rest,authAws);
CkRestW_AddQueryParam(rest,L"Version",L"2011-06-15");
CkRestW_AddQueryParam(rest,L"Action",L"GetSessionToken");
CkRestW_AddQueryParam(rest,L"DurationSeconds",L"3600");
responseXml = CkRestW_fullRequestNoBody(rest,L"GET",L"/");
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
return;
}
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response body: %s\n",responseXml);
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
return;
}
// Examine the successful XML response (shown below)
xml = CkXmlW_Create();
CkXmlW_LoadXml(xml,responseXml);
wprintf(L"%s\n",CkXmlW_getXml(xml));
// 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:
GetSessionTokenResponse_xmlns = CkXmlW_getAttrValue(xml,L"xmlns");
AccessKeyId = CkXmlW_getChildContent(xml,L"GetSessionTokenResult|Credentials|AccessKeyId");
SecretAccessKey = CkXmlW_getChildContent(xml,L"GetSessionTokenResult|Credentials|SecretAccessKey");
SessionToken = CkXmlW_getChildContent(xml,L"GetSessionTokenResult|Credentials|SessionToken");
Expiration = CkXmlW_getChildContent(xml,L"GetSessionTokenResult|Credentials|Expiration");
RequestId = CkXmlW_getChildContent(xml,L"ResponseMetadata|RequestId");
// Save the session token XML to a file for use by another Chilkat example..
success = CkXmlW_SaveXml(xml,L"qa_data/tokens/aws_session_token.xml");
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkXmlW_Dispose(xml);
}