Sample code for 30+ languages & platforms
DataFlex

AzureWebsites OAuth2 Password Flow

See more OAuth2 Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vReq
    Handle hoReq
    String sTokenEndpoint
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    Variant vSbXml
    Handle hoSbXml
    String sDestUrl
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Set ComHttpVerb Of hoReq To "POST"
    Set ComPath Of hoReq To "/token"
    Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
    Send ComAddParam To hoReq "grant_type" "password"
    Send ComAddParam To hoReq "username" "your_username"
    Send ComAddParam To hoReq "password" "your_password"

    Move "https://your_api.azurewebsites.net/token" To sTokenEndpoint

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp sTokenEndpoint vReq vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    // 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"
    // }

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

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

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get ComLoadFile Of hoSbXml "c:/someDir/someXmlFile.xml" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load the XML file."
        Procedure_Return
    End

    // Get the OAuth2 token and use it for authentication
    Get ComStringOf Of hoJResp "token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1

    Move "https://your_api.azurewebsites.net/destinationUrl" To sDestUrl
    Get pvComObject of hoSbXml to vSbXml
    Get pvComObject of hoResp to vResp
    Get ComHttpSb Of hoHttp "POST" sDestUrl vSbXml "utf-8" "application/xml" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    // Examine the response body
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1


End_Procedure