Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL cResponseXml
LOCAL oXml
LOCAL cGetSessionTokenResponse_xmlns
LOCAL cAccessKeyId
LOCAL cSecretAccessKey
LOCAL cSessionToken
LOCAL cExpiration
LOCAL cRequestId

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oRest := CreateObject("Chilkat.Rest")

//  Connect to the Amazon AWS REST server.
//  such as https://sts.us-west-2.amazonaws.com/
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("sts.us-west-2.amazonaws.com", nPort, nBTls, nBAutoReconnect)

//  Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws: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
oAuthAws:Region := "us-west-2"
oAuthAws:ServiceName := "sts"

oRest:SetAuthAws(oAuthAws)

oRest:AddQueryParam("Version", "2011-06-15")
oRest:AddQueryParam("Action", "GetSessionToken")
oRest:AddQueryParam("DurationSeconds", "3600")

cResponseXml := oRest:FullRequestNoBody("GET", "/")
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

//  A successful response will have a status code equal to 200.
IF (oRest:ResponseStatusCode != 200)
    ? "response status code = " + Str(oRest:ResponseStatusCode)
    ? "response status text = " + oRest:ResponseStatusText
    ? "response header: " + oRest:ResponseHeader
    ? "response body: " + cResponseXml
    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

//  Examine the successful XML response (shown below)
oXml := CreateObject("Chilkat.Xml")
oXml:LoadXml(cResponseXml)
? oXml: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:
cGetSessionTokenResponse_xmlns := oXml:GetAttrValue("xmlns")
cAccessKeyId := oXml:GetChildContent("GetSessionTokenResult|Credentials|AccessKeyId")
cSecretAccessKey := oXml:GetChildContent("GetSessionTokenResult|Credentials|SecretAccessKey")
cSessionToken := oXml:GetChildContent("GetSessionTokenResult|Credentials|SessionToken")
cExpiration := oXml:GetChildContent("GetSessionTokenResult|Credentials|Expiration")
cRequestId := oXml:GetChildContent("ResponseMetadata|RequestId")

//  Save the session token XML to a file for use by another Chilkat example..
nSuccess := oXml:SaveXml("qa_data/tokens/aws_session_token.xml")

oRest:destroy()
oAuthAws:destroy()
oXml:destroy()