Azure Table: Read an Entity with Entra Client Credentials
See more Azure Table Service Examples
Supply tenant_id (directory GUID), client_id and client_secret in private storage.json. This confidential-client flow requests the Storage .default scope; it has no refresh token. Assign Storage Table Data Reader or an appropriate Table data role. A management-plane Contributor role alone is not sufficient. Acquires a Storage-audience token and actually uses it to retrieve an entity, without an account key.
Saves only access_token, expires_at and resource to private table-token.json, replacing the prior cache. Uses request-start time for expiry. Reacquire before expiry; do not print the token or embed a client secret in a public client. This sample stops on identity errors rather than applying a generic retry policy.
Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.
HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- StringBuilder compares text by value in every generated language.
local textCompare = chilkat.StringBuilder.new()
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local config = chilkat.JsonObject.new()
config.strict = true
success = config:load_file("qa_data/azure/storage.json")
if not success then
print("Could not load private Azure configuration.")
return
end
textCompare:clear()
textCompare:set_string(config:string_of("account"))
local sameText1 = textCompare:contents_equal("", true)
if (config:json_type_of("account") ~= 1) or (sameText1) then
print("Supply the storage account name.")
return
end
local identity = chilkat.Rest.new()
identity.connect_timeout_ms = 5000
identity.idle_timeout_ms = 30000
local expires = chilkat.DateTime.new()
success = expires:set_from_current_system_time()
if not success then
print("Could not prepare the request.")
return
end
textCompare:clear()
textCompare:set_string(config:string_of("tenant_id"))
local sameText2 = textCompare:contents_equal("", true)
if (config:json_type_of("tenant_id") ~= 1) or (sameText2) then
print("Missing string tenant_id.")
return
end
textCompare:clear()
textCompare:set_string(config:string_of("client_id"))
local sameText3 = textCompare:contents_equal("", true)
if (config:json_type_of("client_id") ~= 1) or (sameText3) then
print("Missing string client_id.")
return
end
textCompare:clear()
textCompare:set_string(config:string_of("client_secret"))
local sameText4 = textCompare:contents_equal("", true)
if (config:json_type_of("client_secret") ~= 1) or (sameText4) then
print("Missing string client_secret.")
return
end
local validate1 = chilkat.StringBuilder.new()
local alphabet1 = chilkat.StringBuilder.new()
success = validate1:append(config:string_of("tenant_id"))
if not success then
print("Could not prepare the request.")
return
end
success = alphabet1:append("0123456789abcdefABCDEF-")
if not success then
print("Could not prepare the request.")
return
end
if (validate1.length < 36) or (validate1.length > 36) then
print("Invalid tenant GUID length.")
return
end
local index1 = 0
while index1 < validate1.length do
if not alphabet1:contains(validate1:get_range(index1, 1, false), true) then
print("Invalid tenant GUID.")
return
end
index1 = index1 + 1
end
local identityTls = chilkat.Socket.new()
identityTls.require_ssl_cert_verify = true
identityTls.max_read_idle_ms = 30000
identityTls.max_send_idle_ms = 30000
success = identityTls:connect("login.microsoftonline.com", 443, true, 20000)
if not success then
print("Identity TLS connection failed.")
return
end
success = identity:use_connection(identityTls, false)
if not success then
print("Could not prepare the request.")
return
end
local tokenPath = chilkat.StringBuilder.new()
success = tokenPath:append("/")
if not success then
print("Could not prepare the request.")
return
end
success = tokenPath:append(config:string_of("tenant_id"))
if not success then
print("Could not prepare the request.")
return
end
success = tokenPath:append("/oauth2/v2.0/token")
if not success then
print("Could not prepare the request.")
return
end
success = identity:add_query_param("grant_type", "client_credentials")
if not success then
print("Could not prepare the request.")
return
end
success = identity:add_query_param("scope", "https://storage.azure.com/.default")
if not success then
print("Could not prepare the request.")
return
end
success = identity:add_query_param("client_id", config:string_of("client_id"))
if not success then
print("Could not prepare the request.")
return
end
success = identity:add_query_param("client_secret", config:string_of("client_secret"))
if not success then
print("Could not prepare the request.")
return
end
-- FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
local tokenText = identity:full_request_form_url_encoded("POST", tokenPath:get_as_string())
if not identity.last_method_success then
print("Token request did not complete.")
return
end
if identity.response_status_code ~= 200 then
print("Token request failed; keep its body and diagnostics private.")
return
end
local token = chilkat.JsonObject.new()
token.strict = true
success = token:load(tokenText)
if not success then
print("Expected token JSON.")
return
end
textCompare:clear()
textCompare:set_string(token:string_of("access_token"))
local sameText5 = textCompare:contents_equal("", true)
if (token:json_type_of("access_token") ~= 1) or (sameText5) then
print("Missing string access_token.")
return
end
textCompare:clear()
textCompare:set_string(token:string_of("token_type"))
local sameText6 = textCompare:contents_equal("Bearer", true)
if not sameText6 then
print("Expected a bearer token.")
return
end
if token:json_type_of("expires_in") ~= 2 then
print("Invalid token lifetime type.")
return
end
local validate2 = chilkat.StringBuilder.new()
local alphabet2 = chilkat.StringBuilder.new()
success = validate2:append(token:string_of("expires_in"))
if not success then
print("Could not prepare the request.")
return
end
success = alphabet2:append("0123456789")
if not success then
print("Could not prepare the request.")
return
end
if (validate2.length < 1) or (validate2.length > 6) then
print("Invalid token lifetime length.")
return
end
local index2 = 0
while index2 < validate2.length do
if not alphabet2:contains(validate2:get_range(index2, 1, false), true) then
print("Invalid token lifetime.")
return
end
index2 = index2 + 1
end
local seconds = token:int_of("expires_in")
if (seconds < 120) or (seconds > 86400) then
print("Unexpected token lifetime.")
return
end
success = expires:add_seconds(seconds)
if not success then
print("Could not prepare the request.")
return
end
local now = chilkat.DateTime.new()
success = now:set_from_current_system_time()
if not success then
print("Could not prepare the request.")
return
end
if expires:diff_seconds(now) <= 60 then
print("Token expired while the request was in progress.")
return
end
local saved = chilkat.JsonObject.new()
success = saved:update_string("access_token", token:string_of("access_token"))
if not success then
print("Could not prepare the request.")
return
end
success = saved:update_string("expires_at", expires:get_as_timestamp(false))
if not success then
print("Could not prepare the request.")
return
end
success = saved:update_string("resource", "https://storage.azure.com/")
if not success then
print("Could not prepare the request.")
return
end
success = saved:write_file("qa_data/azure/table-token.json")
if not success then
print("Could not save the private token file.")
return
end
local accountText = chilkat.StringBuilder.new()
success = accountText:append(config:string_of("account"))
if not success then
print("Could not prepare the request.")
return
end
if (accountText.length < 3) or (accountText.length > 24) then
print("Storage account names need 3-24 lowercase letters or digits.")
return
end
local allowedAccountChars = chilkat.StringBuilder.new()
success = allowedAccountChars:append("abcdefghijklmnopqrstuvwxyz0123456789")
if not success then
print("Could not prepare the request.")
return
end
local charIndex = 0
while charIndex < accountText.length do
if not allowedAccountChars:contains(accountText:get_range(charIndex, 1, false), true) then
print("Storage account names need lowercase letters or digits.")
return
end
charIndex = charIndex + 1
end
local endpoint = chilkat.StringBuilder.new()
success = endpoint:append(config:string_of("account"))
if not success then
print("Could not prepare the request.")
return
end
success = endpoint:append(".table.core.windows.net")
if not success then
print("Could not prepare the request.")
return
end
-- Table names contain only letters and digits and are case-insensitive.
-- Entity PartitionKey and RowKey values are case-sensitive.
local rest = chilkat.Rest.new()
rest.connect_timeout_ms = 20000
rest.idle_timeout_ms = 60000
local connection = chilkat.Socket.new()
connection.require_ssl_cert_verify = true
connection.max_read_idle_ms = 60000
connection.max_send_idle_ms = 60000
success = connection:connect(endpoint:get_as_string(), 443, true, 20000)
if not success then
print("TLS connection failed; inspect diagnostics privately.")
return
end
-- Do not replay a write automatically after losing its response.
success = rest:use_connection(connection, false)
if not success then
print("Could not prepare the request.")
return
end
local result = chilkat.JsonObject.new()
result.strict = true
success = rest:add_header("DataServiceVersion", "3.0;NetFx")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("MaxDataServiceVersion", "3.0;NetFx")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("Content-Type", "application/json")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("Accept", "application/json;odata=minimalmetadata")
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("x-ms-version", "2026-04-06")
if not success then
print("Could not prepare the request.")
return
end
local authorization = chilkat.StringBuilder.new()
success = authorization:append("Bearer ")
if not success then
print("Could not prepare the request.")
return
end
success = authorization:append(token:string_of("access_token"))
if not success then
print("Could not prepare the request.")
return
end
success = rest:add_header("Authorization", authorization:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
local partitionKey = "customers-west"
local rowKey = "customer-001"
local encodedPk = chilkat.StringBuilder.new()
success = encodedPk:append(partitionKey)
if not success then
print("Could not prepare the request.")
return
end
local escapedencodedPk = encodedPk:replace("'", "''")
success = encodedPk:encode("url_rfc3986", "utf-8")
if not success then
print("Could not prepare the request.")
return
end
local encodedRk = chilkat.StringBuilder.new()
success = encodedRk:append(rowKey)
if not success then
print("Could not prepare the request.")
return
end
local escapedencodedRk = encodedRk:replace("'", "''")
success = encodedRk:encode("url_rfc3986", "utf-8")
if not success then
print("Could not prepare the request.")
return
end
-- Escape apostrophes for OData first, then percent-encode each key once.
local entityPath = chilkat.StringBuilder.new()
success = entityPath:append("/ExampleCustomers(PartitionKey='")
if not success then
print("Could not prepare the request.")
return
end
success = entityPath:append(encodedPk:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
success = entityPath:append("',RowKey='")
if not success then
print("Could not prepare the request.")
return
end
success = entityPath:append(encodedRk:get_as_string())
if not success then
print("Could not prepare the request.")
return
end
success = entityPath:append("')")
if not success then
print("Could not prepare the request.")
return
end
local resourcePath = entityPath:get_as_string()
success = now:set_from_current_system_time()
if not success then
print("Could not prepare the request.")
return
end
if expires:diff_seconds(now) <= 60 then
print("Reacquire a token before the Table request.")
return
end
local responseText = rest:full_request_no_body("GET", resourcePath)
if not rest.last_method_success then
print("Request did not complete; inspect the target before retrying a write.")
return
end
if rest.response_status_code ~= 200 then
print("HTTP status: " .. rest.response_status_code)
print("Request ID: " .. rest:response_hdr_by_name("x-ms-request-id"))
-- 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
-- Keep response bodies private. Reconcile uncertain writes before retrying.
return
end
success = result:load(responseText)
if not success then
print("Expected well-formed JSON.")
return
end
textCompare:clear()
textCompare:set_string(result:string_of("PartitionKey"))
local sameText7 = textCompare:contents_equal("", true)
if (result:json_type_of("PartitionKey") ~= 1) or (sameText7) then
print("Missing string PartitionKey.")
return
end
textCompare:clear()
textCompare:set_string(result:string_of("RowKey"))
local sameText8 = textCompare:contents_equal("", true)
if (result:json_type_of("RowKey") ~= 1) or (sameText8) then
print("Missing string RowKey.")
return
end
textCompare:clear()
textCompare:set_string(result:string_of("PartitionKey"))
local sameText9 = textCompare:contents_equal(partitionKey, true)
textCompare:clear()
textCompare:set_string(result:string_of("RowKey"))
local sameText10 = textCompare:contents_equal(rowKey, true)
if (not sameText9) or (not sameText10) then
print("Returned entity keys do not match.")
return
end
print("Storage token obtained and Table entity retrieved.")