Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Config
long li_SameText1
oleobject loo_Identity
oleobject loo_Expires
long li_SameText2
long li_SameText3
long li_SameText4
oleobject loo_IdentityTls
oleobject loo_TokenPath
string ls_TokenText
oleobject loo_Token
long li_SameText5
long li_SameText6
long li_Seconds
string ls_AccessToken
oleobject loo_Endpoint
string ls_ResourcePath
oleobject loo_Rest
oleobject loo_Connection
string ls_ResponseText
oleobject loo_Result
oleobject loo_Bearer

li_Success = 0

//  StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TextCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")

loo_Config.Strict = 1
li_Success = loo_Config.LoadFile("qa_data/azure/storage.json")
if li_Success <> 1 then
    Write-Debug "Could not load private Azure configuration."
    destroy loo_TextCompare
    destroy loo_Config
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("account"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("account") <> 1) OR (li_SameText1 = 1) then
    Write-Debug "Supply the storage account name."
    destroy loo_TextCompare
    destroy loo_Config
    return
end if

loo_Identity = create oleobject
li_rc = loo_Identity.ConnectToNewObject("Chilkat.Rest")

loo_Identity.ConnectTimeoutMs = 20000
loo_Identity.IdleTimeoutMs = 60000
loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Expires.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("tenant_id"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("tenant_id") <> 1) OR (li_SameText2 = 1) then
    Write-Debug "Supply tenant_id in private storage.json."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("client_id"))
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("client_id") <> 1) OR (li_SameText3 = 1) then
    Write-Debug "Supply client_id in private storage.json."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Config.StringOf("client_secret"))
li_SameText4 = loo_TextCompare.ContentsEqual("",1)
if (loo_Config.JsonTypeOf("client_secret") <> 1) OR (li_SameText4 = 1) then
    Write-Debug "Supply client_secret in private storage.json."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    return
end if

loo_IdentityTls = create oleobject
li_rc = loo_IdentityTls.ConnectToNewObject("Chilkat.Socket")

loo_IdentityTls.RequireSslCertVerify = 1
li_Success = loo_IdentityTls.Connect("login.microsoftonline.com",443,1,20000)
if li_Success <> 1 then
    Write-Debug "Identity TLS connection failed."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    return
end if

li_Success = loo_Identity.UseConnection(loo_IdentityTls,0)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    return
end if

loo_TokenPath = create oleobject
li_rc = loo_TokenPath.ConnectToNewObject("Chilkat.StringBuilder")

//  tenant_id is the application's directory GUID, not common or organizations.
li_Success = loo_TokenPath.Append("/")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_TokenPath.Append(loo_Config.StringOf("tenant_id"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_TokenPath.Append("/oauth2/v2.0/token")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_Identity.AddQueryParam("grant_type","client_credentials")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_Identity.AddQueryParam("client_id",loo_Config.StringOf("client_id"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_Identity.AddQueryParam("client_secret",loo_Config.StringOf("client_secret"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

li_Success = loo_Identity.AddQueryParam("scope","https://storage.azure.com/.default")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

//  FullRequestFormUrlEncoded serializes these parameters in the POST body.
ls_TokenText = loo_Identity.FullRequestFormUrlEncoded("POST",loo_TokenPath.GetAsString())
if loo_Identity.LastMethodSuccess <> 1 then
    Write-Debug "Token request did not complete; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

if loo_Identity.ResponseStatusCode <> 200 then
    Write-Debug "Token request failed; inspect the status privately, never print the response token."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    return
end if

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")

loo_Token.Strict = 1
li_Success = loo_Token.Load(ls_TokenText)
if li_Success <> 1 then
    Write-Debug "Expected token JSON."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Token.StringOf("access_token"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Token.StringOf("token_type"))
li_SameText6 = loo_TextCompare.ContentsEqual("Bearer",1)
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (li_SameText5 = 1) OR (li_SameText6 <> 1) then
    Write-Debug "Missing bearer token."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

li_Seconds = loo_Token.IntOf("expires_in")
if (li_Seconds < 120) OR (li_Seconds > 86400) then
    Write-Debug "Unexpected token lifetime."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

li_Success = loo_Expires.AddSeconds(li_Seconds)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

li_Success = loo_Token.UpdateString("expires_at",loo_Expires.GetAsTimestamp(0))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

li_Success = loo_Token.WriteFile("qa_data/azure/storage-token.json")
if li_Success <> 1 then
    Write-Debug "Could not persist token privately."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    return
end if

ls_AccessToken = loo_Token.StringOf("access_token")
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Endpoint.Append(loo_Config.StringOf("account"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".blob.core.windows.net")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    return
end if

//  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
//  Keep slash separators; names and paths are case-sensitive.
ls_ResourcePath = "/examplecontainer/example.bin"
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    return
end if

//  Do not replay a write automatically after losing its response.
li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.Xml")

loo_Result.Strict = 1
li_Success = loo_Rest.AddHeader("x-ms-version","2026-04-06")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    return
end if

loo_Bearer = create oleobject
li_rc = loo_Bearer.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Bearer.Append("Bearer ")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    destroy loo_Bearer
    return
end if

li_Success = loo_Bearer.Append(ls_AccessToken)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    destroy loo_Bearer
    return
end if

li_Success = loo_Rest.AddHeader("Authorization",loo_Bearer.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    destroy loo_Bearer
    return
end if

ls_ResponseText = loo_Rest.FullRequestNoBody("HEAD",ls_ResourcePath)
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "Request did not complete; outcome may be uncertain."
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    destroy loo_Bearer
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "HTTP status: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "Azure error code: " + loo_Rest.ResponseHdrByName("x-ms-error-code")
    Write-Debug "Request ID: " + loo_Rest.ResponseHdrByName("x-ms-request-id")
    //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    destroy loo_TextCompare
    destroy loo_Config
    destroy loo_Identity
    destroy loo_Expires
    destroy loo_IdentityTls
    destroy loo_TokenPath
    destroy loo_Token
    destroy loo_Endpoint
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Result
    destroy loo_Bearer
    return
end if

Write-Debug "Storage token obtained and blob properties read."
Write-Debug "ETag: " + loo_Rest.ResponseHdrByName("ETag")


destroy loo_TextCompare
destroy loo_Config
destroy loo_Identity
destroy loo_Expires
destroy loo_IdentityTls
destroy loo_TokenPath
destroy loo_Token
destroy loo_Endpoint
destroy loo_Rest
destroy loo_Connection
destroy loo_Result
destroy loo_Bearer