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.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loConfig
LOCAL lnSameText1
LOCAL loIdentity
LOCAL loExpires
LOCAL lnSameText2
LOCAL lnSameText3
LOCAL lnSameText4
LOCAL loIdentityTls
LOCAL loTokenPath
LOCAL lcTokenText
LOCAL loToken
LOCAL lnSameText5
LOCAL lnSameText6
LOCAL lnSeconds
LOCAL lcAccessToken
LOCAL loEndpoint
LOCAL lcResourcePath
LOCAL loRest
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResult
LOCAL loBearer
lnSuccess = 0
* StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/storage.json")
IF (lnSuccess <> 1) THEN
? "Could not load private Azure configuration."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("account"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("account") <> 1) OR (lnSameText1 = 1)) THEN
? "Supply the storage account name."
RELEASE loTextCompare
RELEASE loConfig
CANCEL
ENDIF
loIdentity = CreateObject('Chilkat.Rest')
loIdentity.ConnectTimeoutMs = 20000
loIdentity.IdleTimeoutMs = 60000
loExpires = CreateObject('Chilkat.CkDateTime')
lnSuccess = loExpires.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("tenant_id"))
lnSameText2 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("tenant_id") <> 1) OR (lnSameText2 = 1)) THEN
? "Supply tenant_id in private storage.json."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("client_id"))
lnSameText3 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("client_id") <> 1) OR (lnSameText3 = 1)) THEN
? "Supply client_id in private storage.json."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("client_secret"))
lnSameText4 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("client_secret") <> 1) OR (lnSameText4 = 1)) THEN
? "Supply client_secret in private storage.json."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
CANCEL
ENDIF
loIdentityTls = CreateObject('Chilkat.Socket')
loIdentityTls.RequireSslCertVerify = 1
lnSuccess = loIdentityTls.Connect("login.microsoftonline.com",443,1,20000)
IF (lnSuccess <> 1) THEN
? "Identity TLS connection failed."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
CANCEL
ENDIF
lnSuccess = loIdentity.UseConnection(loIdentityTls,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
CANCEL
ENDIF
loTokenPath = CreateObject('Chilkat.StringBuilder')
* tenant_id is the application's directory GUID, not common or organizations.
lnSuccess = loTokenPath.Append("/")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loTokenPath.Append(loConfig.StringOf("tenant_id"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loTokenPath.Append("/oauth2/v2.0/token")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loIdentity.AddQueryParam("grant_type","client_credentials")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loIdentity.AddQueryParam("client_id",loConfig.StringOf("client_id"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loIdentity.AddQueryParam("client_secret",loConfig.StringOf("client_secret"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
lnSuccess = loIdentity.AddQueryParam("scope","https://storage.azure.com/.default")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
* FullRequestFormUrlEncoded serializes these parameters in the POST body.
lcTokenText = loIdentity.FullRequestFormUrlEncoded("POST",loTokenPath.GetAsString())
IF (loIdentity.LastMethodSuccess <> 1) THEN
? "Token request did not complete; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
IF (loIdentity.ResponseStatusCode <> 200) THEN
? "Token request failed; inspect the status privately, never print the response token."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
CANCEL
ENDIF
loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.Load(lcTokenText)
IF (lnSuccess <> 1) THEN
? "Expected token JSON."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
loTextCompare.Clear()
loTextCompare.SetString(loToken.StringOf("access_token"))
lnSameText5 = loTextCompare.ContentsEqual("",1)
loTextCompare.Clear()
loTextCompare.SetString(loToken.StringOf("token_type"))
lnSameText6 = loTextCompare.ContentsEqual("Bearer",1)
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (lnSameText5 = 1) OR (lnSameText6 <> 1)) THEN
? "Missing bearer token."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
lnSeconds = loToken.IntOf("expires_in")
IF ((lnSeconds < 120) OR (lnSeconds > 86400)) THEN
? "Unexpected token lifetime."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
lnSuccess = loExpires.AddSeconds(lnSeconds)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
lnSuccess = loToken.UpdateString("expires_at",loExpires.GetAsTimestamp(0))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
lnSuccess = loToken.WriteFile("qa_data/azure/storage-token.json")
IF (lnSuccess <> 1) THEN
? "Could not persist token privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
CANCEL
ENDIF
lcAccessToken = loToken.StringOf("access_token")
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(loConfig.StringOf("account"))
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
CANCEL
ENDIF
lnSuccess = loEndpoint.Append(".blob.core.windows.net")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
CANCEL
ENDIF
* resourcePath is already URL-encoded. Encode each path segment once when adapting it.
* Keep slash separators; names and paths are case-sensitive.
lcResourcePath = "/examplecontainer/example.bin"
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
* Do not replay a write automatically after losing its response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
loResult = CreateObject('Chilkat.Xml')
loResult.Strict = 1
lnSuccess = loRest.AddHeader("x-ms-version","2026-04-06")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
CANCEL
ENDIF
loBearer = CreateObject('Chilkat.StringBuilder')
lnSuccess = loBearer.Append("Bearer ")
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer
CANCEL
ENDIF
lnSuccess = loBearer.Append(lcAccessToken)
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer
CANCEL
ENDIF
lnSuccess = loRest.AddHeader("Authorization",loBearer.GetAsString())
IF (lnSuccess <> 1) THEN
? "Could not prepare the request."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer
CANCEL
ENDIF
lcResponseText = loRest.FullRequestNoBody("HEAD",lcResourcePath)
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; outcome may be uncertain."
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "HTTP status: " + STR(loRest.ResponseStatusCode)
? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
* Inspect any error body privately; do not log credentials, SAS URLs or payloads.
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer
CANCEL
ENDIF
? "Storage token obtained and blob properties read."
? "ETag: " + loRest.ResponseHdrByName("ETag")
RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loIdentityTls
RELEASE loTokenPath
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer