Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oRest = ObjCreate("Chilkat.Rest")

;  Connect to the Amazon AWS REST server.
;  such as https://sts.us-west-2.amazonaws.com/
Local $bTls = True
Local $iPort = 443
Local $bAutoReconnect = True
$bSuccess = $oRest.Connect("sts.us-west-2.amazonaws.com",$iPort,$bTls,$bAutoReconnect)

;  Provide AWS credentials for the REST call.
$oAuthAws = ObjCreate("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")

Local $sResponseXml = $oRest.FullRequestNoBody("GET","/")
If ($oRest.LastMethodSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

;  A successful response will have a status code equal to 200.
If ($oRest.ResponseStatusCode <> 200) Then
    ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF)
    ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF)
    ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
    ConsoleWrite("response body: " & $sResponseXml & @CRLF)
    Exit
EndIf

;  Examine the successful XML response (shown below)
$oXml = ObjCreate("Chilkat.Xml")
$oXml.LoadXml($sResponseXml)
ConsoleWrite($oXml.GetXml() & @CRLF)

;  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:
Local $sGetSessionTokenResponse_xmlns = $oXml.GetAttrValue("xmlns")
Local $sAccessKeyId = $oXml.GetChildContent("GetSessionTokenResult|Credentials|AccessKeyId")
Local $sSecretAccessKey = $oXml.GetChildContent("GetSessionTokenResult|Credentials|SecretAccessKey")
Local $sSessionToken = $oXml.GetChildContent("GetSessionTokenResult|Credentials|SessionToken")
Local $sExpiration = $oXml.GetChildContent("GetSessionTokenResult|Credentials|Expiration")
Local $sRequestId = $oXml.GetChildContent("ResponseMetadata|RequestId")

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