Sample code for 30+ languages & platforms
AutoIt

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

Demonstrates how to do OAuth 2.0 password flow for azurewebsites.net.

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.

$oHttp = ObjCreate("Chilkat.Http")

$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "POST"
$oReq.Path = "/token"
$oReq.ContentType = "application/x-www-form-urlencoded"
$oReq.AddParam "grant_type","password"
$oReq.AddParam "username","your_username"
$oReq.AddParam "password","your_password"

Local $sTokenEndpoint = "https://your_api.azurewebsites.net/token"

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpReq($sTokenEndpoint,$oReq,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)
$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

; Sample JSON response:

; {
;   "access_token": "NQGHn ... xTS",
;   "token_type": "bearer",
;   "expires_in": 1209599,
;   "userName": "your_username",
;   ".issued": "Mon, 27 Apr 2020 23:49:35 GMT",
;   ".expires": "Mon, 11 May 2020 23:49:35 GMT"
; }

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
EndIf

; ----------------------------------
; Use the OAuth2 token in a request.
; For example...

$oSbXml = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oSbXml.LoadFile("c:/someDir/someXmlFile.xml","utf-8")
If ($bSuccess = False) Then
    ConsoleWrite("Failed to load the XML file." & @CRLF)
    Exit
EndIf

; Get the OAuth2 token and use it for authentication
$oHttp.AuthToken = $oJResp.StringOf("token")

Local $sDestUrl = "https://your_api.azurewebsites.net/destinationUrl"
$bSuccess = $oHttp.HttpSb("POST",$sDestUrl,$oSbXml,"utf-8","application/xml",$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
EndIf

; Examine the response body
ConsoleWrite($oResp.BodyStr & @CRLF)