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

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.

Microsoft reference.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
textCompare: TChilkatStringBuilder;
config: TChilkatJsonObject;
sameText1: Integer;
identity: TChilkatRest;
expires: TCkDateTime;
sameText2: Integer;
sameText3: Integer;
sameText4: Integer;
validate1: TChilkatStringBuilder;
alphabet1: TChilkatStringBuilder;
index1: Integer;
identityTls: TChilkatSocket;
tokenPath: TChilkatStringBuilder;
tokenText: WideString;
token: TChilkatJsonObject;
sameText5: Integer;
sameText6: Integer;
validate2: TChilkatStringBuilder;
alphabet2: TChilkatStringBuilder;
index2: Integer;
seconds: Integer;
now: TCkDateTime;
saved: TChilkatJsonObject;
accountText: TChilkatStringBuilder;
allowedAccountChars: TChilkatStringBuilder;
charIndex: Integer;
endpoint: TChilkatStringBuilder;
resourcePath: WideString;
rest: TChilkatRest;
connection: TChilkatSocket;
responseText: WideString;
result: TChilkatJsonObject;
authorization: TChilkatStringBuilder;
partitionKey: WideString;
rowKey: WideString;
encodedPk: TChilkatStringBuilder;
escapedencodedPk: Integer;
encodedRk: TChilkatStringBuilder;
escapedencodedRk: Integer;
entityPath: TChilkatStringBuilder;
sameText7: Integer;
sameText8: Integer;
sameText9: Integer;
sameText10: Integer;

begin
success := 0;

//  StringBuilder compares text by value in every generated language.
textCompare := TChilkatStringBuilder.Create(Self);
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
config := TChilkatJsonObject.Create(Self);
config.Strict := 1;
success := config.LoadFile('qa_data/azure/storage.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load private Azure configuration.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(config.StringOf('account'));
sameText1 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('account') <> 1) or (sameText1 = 1)) then
  begin
    Memo1.Lines.Add('Supply the storage account name.');
    Exit;
  end;
identity := TChilkatRest.Create(Self);
identity.ConnectTimeoutMs := 5000;
identity.IdleTimeoutMs := 30000;
expires := TCkDateTime.Create(Self);
success := expires.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(config.StringOf('tenant_id'));
sameText2 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('tenant_id') <> 1) or (sameText2 = 1)) then
  begin
    Memo1.Lines.Add('Missing string tenant_id.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(config.StringOf('client_id'));
sameText3 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('client_id') <> 1) or (sameText3 = 1)) then
  begin
    Memo1.Lines.Add('Missing string client_id.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(config.StringOf('client_secret'));
sameText4 := textCompare.ContentsEqual('',1);
if ((config.JsonTypeOf('client_secret') <> 1) or (sameText4 = 1)) then
  begin
    Memo1.Lines.Add('Missing string client_secret.');
    Exit;
  end;
validate1 := TChilkatStringBuilder.Create(Self);
alphabet1 := TChilkatStringBuilder.Create(Self);
success := validate1.Append(config.StringOf('tenant_id'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := alphabet1.Append('0123456789abcdefABCDEF-');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
if ((validate1.Length < 36) or (validate1.Length > 36)) then
  begin
    Memo1.Lines.Add('Invalid tenant GUID length.');
    Exit;
  end;
index1 := 0;
while (index1 < validate1.Length) do
  begin
    if (alphabet1.Contains(validate1.GetRange(index1,1,0),1) <> 1) then
      begin
        Memo1.Lines.Add('Invalid tenant GUID.');
        Exit;
      end;
    index1 := index1 + 1;
  end;

identityTls := TChilkatSocket.Create(Self);
identityTls.RequireSslCertVerify := 1;
identityTls.MaxReadIdleMs := 30000;
identityTls.MaxSendIdleMs := 30000;
success := identityTls.Connect('login.microsoftonline.com',443,1,20000);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Identity TLS connection failed.');
    Exit;
  end;
success := identity.UseConnection(identityTls.ControlInterface,0);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
tokenPath := TChilkatStringBuilder.Create(Self);
success := tokenPath.Append('/');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := tokenPath.Append(config.StringOf('tenant_id'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := tokenPath.Append('/oauth2/v2.0/token');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := identity.AddQueryParam('grant_type','client_credentials');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := identity.AddQueryParam('scope','https://storage.azure.com/.default');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := identity.AddQueryParam('client_id',config.StringOf('client_id'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := identity.AddQueryParam('client_secret',config.StringOf('client_secret'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
//  FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
tokenText := identity.FullRequestFormUrlEncoded('POST',tokenPath.GetAsString());
if (identity.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Token request did not complete.');
    Exit;
  end;
if (identity.ResponseStatusCode <> 200) then
  begin
    Memo1.Lines.Add('Token request failed; keep its body and diagnostics private.');
    Exit;
  end;
token := TChilkatJsonObject.Create(Self);
token.Strict := 1;
success := token.Load(tokenText);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected token JSON.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(token.StringOf('access_token'));
sameText5 := textCompare.ContentsEqual('',1);
if ((token.JsonTypeOf('access_token') <> 1) or (sameText5 = 1)) then
  begin
    Memo1.Lines.Add('Missing string access_token.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(token.StringOf('token_type'));
sameText6 := textCompare.ContentsEqual('Bearer',1);
if (sameText6 <> 1) then
  begin
    Memo1.Lines.Add('Expected a bearer token.');
    Exit;
  end;
if ((token.JsonTypeOf('expires_in') <> 2)) then
  begin
    Memo1.Lines.Add('Invalid token lifetime type.');
    Exit;
  end;
validate2 := TChilkatStringBuilder.Create(Self);
alphabet2 := TChilkatStringBuilder.Create(Self);
success := validate2.Append(token.StringOf('expires_in'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := alphabet2.Append('0123456789');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
if ((validate2.Length < 1) or (validate2.Length > 6)) then
  begin
    Memo1.Lines.Add('Invalid token lifetime length.');
    Exit;
  end;
index2 := 0;
while (index2 < validate2.Length) do
  begin
    if (alphabet2.Contains(validate2.GetRange(index2,1,0),1) <> 1) then
      begin
        Memo1.Lines.Add('Invalid token lifetime.');
        Exit;
      end;
    index2 := index2 + 1;
  end;

seconds := token.IntOf('expires_in');
if ((seconds < 120) or (seconds > 86400)) then
  begin
    Memo1.Lines.Add('Unexpected token lifetime.');
    Exit;
  end;
success := expires.AddSeconds(seconds);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
now := TCkDateTime.Create(Self);
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
if (expires.DiffSeconds(now.ControlInterface) <= 60) then
  begin
    Memo1.Lines.Add('Token expired while the request was in progress.');
    Exit;
  end;
saved := TChilkatJsonObject.Create(Self);
success := saved.UpdateString('access_token',token.StringOf('access_token'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := saved.UpdateString('expires_at',expires.GetAsTimestamp(0));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := saved.UpdateString('resource','https://storage.azure.com/');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := saved.WriteFile('qa_data/azure/table-token.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not save the private token file.');
    Exit;
  end;
accountText := TChilkatStringBuilder.Create(Self);
success := accountText.Append(config.StringOf('account'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
if ((accountText.Length < 3) or (accountText.Length > 24)) then
  begin
    Memo1.Lines.Add('Storage account names need 3-24 lowercase letters or digits.');
    Exit;
  end;
allowedAccountChars := TChilkatStringBuilder.Create(Self);
success := allowedAccountChars.Append('abcdefghijklmnopqrstuvwxyz0123456789');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
charIndex := 0;
while (charIndex < accountText.Length) do
  begin
    if (allowedAccountChars.Contains(accountText.GetRange(charIndex,1,0),1) <> 1) then
      begin
        Memo1.Lines.Add('Storage account names need lowercase letters or digits.');
        Exit;
      end;
    charIndex := charIndex + 1;
  end;

endpoint := TChilkatStringBuilder.Create(Self);
success := endpoint.Append(config.StringOf('account'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := endpoint.Append('.table.core.windows.net');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
//  Table names contain only letters and digits and are case-insensitive.
//  Entity PartitionKey and RowKey values are case-sensitive.
resourcePath := '/ExampleCustomers()';
rest := TChilkatRest.Create(Self);
rest.ConnectTimeoutMs := 20000;
rest.IdleTimeoutMs := 60000;
connection := TChilkatSocket.Create(Self);
connection.RequireSslCertVerify := 1;
connection.MaxReadIdleMs := 60000;
connection.MaxSendIdleMs := 60000;
success := connection.Connect(endpoint.GetAsString(),443,1,20000);
if (success <> 1) then
  begin
    Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
    Exit;
  end;
//  Do not replay a write automatically after losing its response.
success := rest.UseConnection(connection.ControlInterface,0);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;

result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
success := rest.AddHeader('DataServiceVersion','3.0;NetFx');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := rest.AddHeader('MaxDataServiceVersion','3.0;NetFx');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := rest.AddHeader('Content-Type','application/json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := rest.AddHeader('Accept','application/json;odata=minimalmetadata');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := rest.AddHeader('x-ms-version','2026-04-06');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
authorization := TChilkatStringBuilder.Create(Self);
success := authorization.Append('Bearer ');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := authorization.Append(token.StringOf('access_token'));
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := rest.AddHeader('Authorization',authorization.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
partitionKey := 'customers-west';
rowKey := 'customer-001';
encodedPk := TChilkatStringBuilder.Create(Self);
success := encodedPk.Append(partitionKey);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
escapedencodedPk := encodedPk.Replace('''','''''');
success := encodedPk.Encode('url_rfc3986','utf-8');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
encodedRk := TChilkatStringBuilder.Create(Self);
success := encodedRk.Append(rowKey);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
escapedencodedRk := encodedRk.Replace('''','''''');
success := encodedRk.Encode('url_rfc3986','utf-8');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
//  Escape apostrophes for OData first, then percent-encode each key once.
entityPath := TChilkatStringBuilder.Create(Self);
success := entityPath.Append('/ExampleCustomers(PartitionKey=''');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := entityPath.Append(encodedPk.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := entityPath.Append(''',RowKey=''');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := entityPath.Append(encodedRk.GetAsString());
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
success := entityPath.Append(''')');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
resourcePath := entityPath.GetAsString();
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not prepare the request.');
    Exit;
  end;
if (expires.DiffSeconds(now.ControlInterface) <= 60) then
  begin
    Memo1.Lines.Add('Reacquire a token before the Table request.');
    Exit;
  end;
responseText := rest.FullRequestNoBody('GET',resourcePath);
if (rest.LastMethodSuccess <> 1) then
  begin
    Memo1.Lines.Add('Request did not complete; inspect the target before retrying a write.');
    Exit;
  end;
if (rest.ResponseStatusCode <> 200) then
  begin
    Memo1.Lines.Add('HTTP status: ' + IntToStr(rest.ResponseStatusCode));
    Memo1.Lines.Add('Request ID: ' + rest.ResponseHdrByName('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.
    Exit;
  end;
success := result.Load(responseText);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Expected well-formed JSON.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(result.StringOf('PartitionKey'));
sameText7 := textCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('PartitionKey') <> 1) or (sameText7 = 1)) then
  begin
    Memo1.Lines.Add('Missing string PartitionKey.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(result.StringOf('RowKey'));
sameText8 := textCompare.ContentsEqual('',1);
if ((result.JsonTypeOf('RowKey') <> 1) or (sameText8 = 1)) then
  begin
    Memo1.Lines.Add('Missing string RowKey.');
    Exit;
  end;
textCompare.Clear();
textCompare.SetString(result.StringOf('PartitionKey'));
sameText9 := textCompare.ContentsEqual(partitionKey,1);
textCompare.Clear();
textCompare.SetString(result.StringOf('RowKey'));
sameText10 := textCompare.ContentsEqual(rowKey,1);
if ((sameText9 <> 1) or (sameText10 <> 1)) then
  begin
    Memo1.Lines.Add('Returned entity keys do not match.');
    Exit;
  end;
Memo1.Lines.Add('Storage token obtained and Table entity retrieved.');