Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.6.0+

Azure Storage: Authenticate with Entra Client Credentials

See more Azure Cloud Storage Examples

Supply account, tenant_id (directory GUID), client_id and client_secret in private qa_data/azure/storage.json. The confidential application obtains an app-only token with the https://storage.azure.com/.default scope, then reads blob properties. Assign a Blob data role, such as Storage Blob Data Reader; a management-plane Contributor role alone does not grant Blob data access. Use this secret-based flow only where the application can keep its credentials private; certificates or managed identities are alternatives.

The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.

Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.

Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.

Microsoft reference.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTextCompare
    Handle hoConfig
    Boolean iSameText1
    Handle hoIdentity
    Handle hoExpires
    Boolean iSameText2
    Boolean iSameText3
    Boolean iSameText4
Tls    Handle hoIdentityTls
Path    Handle hoTokenPath
    String sTokenText
    Handle hoToken
    Boolean iSameText5
    Boolean iSameText6
    Integer iSeconds
    String sAccessToken
    Handle hoEndpoint
    String sResourcePath
    Handle hoRest
    Variant vConnection
    Handle hoConnection
    String sResponseText
    Handle hoResult
    Handle hoBearer
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  StringBuilder compares text by value in every generated language.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoTextCompare
    If (Not(IsComObjectCreated(hoTextCompare))) Begin
        Send CreateComObject of hoTextCompare
    End
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Move False To iSuccess
    Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
    If (Not(IsComObjectCreated(hoConfig))) Begin
        Send CreateComObject of hoConfig
    End
    Set ComStrict Of hoConfig To True
    Get ComLoadFile Of hoConfig "qa_data/azure/storage.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not load private Azure configuration."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "account" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText1
    Get ComJsonTypeOf Of hoConfig "account" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText1 = True)) Begin
        Showln "Supply the storage account name."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatRest)) To hoIdentity
    If (Not(IsComObjectCreated(hoIdentity))) Begin
        Send CreateComObject of hoIdentity
    End
    Set ComConnectTimeoutMs Of hoIdentity To 20000
    Set ComIdleTimeoutMs Of hoIdentity To 60000
    Get Create (RefClass(cComCkDateTime)) To hoExpires
    If (Not(IsComObjectCreated(hoExpires))) Begin
        Send CreateComObject of hoExpires
    End
    Get ComSetFromCurrentSystemTime Of hoExpires To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "tenant_id" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText2
    Get ComJsonTypeOf Of hoConfig "tenant_id" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText2 = True)) Begin
        Showln "Supply tenant_id in private storage.json."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "client_id" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText3
    Get ComJsonTypeOf Of hoConfig "client_id" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText3 = True)) Begin
        Showln "Supply client_id in private storage.json."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoConfig "client_secret" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText4
    Get ComJsonTypeOf Of hoConfig "client_secret" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText4 = True)) Begin
        Showln "Supply client_secret in private storage.json."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatSocket)) To hoIdentityTls
    If (Not(IsComObjectCreated(hoIdentityTls))) Begin
        Send CreateComObject of hoIdentityTls
    End
    Set ComRequireSslCertVerify Of hoIdentityTls To True
    Get ComConnect Of hoIdentityTls "login.microsoftonline.com" 443 True 20000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Identity TLS connection failed."
        Procedure_Return
    End

    Get pvComObject of hoIdentityTls to vIdentityTls
    Get ComUseConnection Of hoIdentity vIdentityTls False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoTokenPath
    If (Not(IsComObjectCreated(hoTokenPath))) Begin
        Send CreateComObject of hoTokenPath
    End
    //  tenant_id is the application's directory GUID, not common or organizations.
    Get ComAppend Of hoTokenPath "/" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComStringOf Of hoConfig "tenant_id" To sTemp1
    Get ComAppend Of hoTokenPath sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoTokenPath "/oauth2/v2.0/token" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddQueryParam Of hoIdentity "grant_type" "client_credentials" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComStringOf Of hoConfig "client_id" To sTemp1
    Get ComAddQueryParam Of hoIdentity "client_id" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComStringOf Of hoConfig "client_secret" To sTemp1
    Get ComAddQueryParam Of hoIdentity "client_secret" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAddQueryParam Of hoIdentity "scope" "https://storage.azure.com/.default" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    //  FullRequestFormUrlEncoded serializes these parameters in the POST body.
    Get ComGetAsString Of hoTokenPath To sTemp1
    Get ComFullRequestFormUrlEncoded Of hoIdentity "POST" sTemp1 To sTokenText
    Get ComLastMethodSuccess Of hoIdentity To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Token request did not complete; inspect diagnostics privately."
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoIdentity To iTemp1
    If (iTemp1 <> 200) Begin
        Showln "Token request failed; inspect the status privately, never print the response token."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Set ComStrict Of hoToken To True
    Get ComLoad Of hoToken sTokenText To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected token JSON."
        Procedure_Return
    End

    Send ComClear To hoTextCompare
    Get ComStringOf Of hoToken "access_token" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "" True To iSameText5
    Send ComClear To hoTextCompare
    Get ComStringOf Of hoToken "token_type" To sTemp1
    Get ComSetString Of hoTextCompare sTemp1 To iSuccess
    Get ComContentsEqual Of hoTextCompare "Bearer" True To iSameText6
    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    If ((iTemp1 <> 1) Or (iSameText5 = True) Or (iSameText6 <> True)) Begin
        Showln "Missing bearer token."
        Procedure_Return
    End

    Get ComIntOf Of hoToken "expires_in" To iSeconds
    If ((iSeconds < 120) Or (iSeconds > 86400)) Begin
        Showln "Unexpected token lifetime."
        Procedure_Return
    End

    Get ComAddSeconds Of hoExpires iSeconds To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsTimestamp Of hoExpires False To sTemp1
    Get ComUpdateString Of hoToken "expires_at" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComWriteFile Of hoToken "qa_data/azure/storage-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not persist token privately."
        Procedure_Return
    End

    Get ComStringOf Of hoToken "access_token" To sAccessToken
    Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
    If (Not(IsComObjectCreated(hoEndpoint))) Begin
        Send CreateComObject of hoEndpoint
    End
    Get ComStringOf Of hoConfig "account" To sTemp1
    Get ComAppend Of hoEndpoint sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoEndpoint ".blob.core.windows.net" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    //  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    //  Keep slash separators; names and paths are case-sensitive.
    Move "/examplecontainer/example.bin" To sResourcePath
    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Set ComConnectTimeoutMs Of hoRest To 20000
    Set ComIdleTimeoutMs Of hoRest To 60000
    Get Create (RefClass(cComChilkatSocket)) To hoConnection
    If (Not(IsComObjectCreated(hoConnection))) Begin
        Send CreateComObject of hoConnection
    End
    Set ComRequireSslCertVerify Of hoConnection To True
    Set ComMaxReadIdleMs Of hoConnection To 60000
    Set ComMaxSendIdleMs Of hoConnection To 60000
    Get ComGetAsString Of hoEndpoint To sTemp1
    Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
    If (iSuccess <> True) Begin
        Showln "TLS connection failed; inspect diagnostics privately."
        Procedure_Return
    End

    //  Do not replay a write automatically after losing its response.
    Get pvComObject of hoConnection to vConnection
    Get ComUseConnection Of hoRest vConnection False To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoResult
    If (Not(IsComObjectCreated(hoResult))) Begin
        Send CreateComObject of hoResult
    End
    Set ComStrict Of hoResult To True
    Get ComAddHeader Of hoRest "x-ms-version" "2026-04-06" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoBearer
    If (Not(IsComObjectCreated(hoBearer))) Begin
        Send CreateComObject of hoBearer
    End
    Get ComAppend Of hoBearer "Bearer " To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComAppend Of hoBearer sAccessToken To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComGetAsString Of hoBearer To sTemp1
    Get ComAddHeader Of hoRest "Authorization" sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not prepare the request."
        Procedure_Return
    End

    Get ComFullRequestNoBody Of hoRest "HEAD" sResourcePath To sResponseText
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Request did not complete; outcome may be uncertain."
        Procedure_Return
    End

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "HTTP status: " iTemp1
        Get ComResponseHdrByName Of hoRest "x-ms-error-code" To sTemp1
        Showln "Azure error code: " sTemp1
        Get ComResponseHdrByName Of hoRest "x-ms-request-id" To sTemp1
        Showln "Request ID: " sTemp1
        //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        Procedure_Return
    End

    Showln "Storage token obtained and blob properties read."
    Get ComResponseHdrByName Of hoRest "ETag" To sTemp1
    Showln "ETag: " sTemp1


End_Procedure