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

Objective-C
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoRest.h>
#import <CkoDateTime.h>
#import <CkoSocket.h>
#import <NSString.h>

BOOL success = NO;

//  StringBuilder compares text by value in every generated language.
CkoStringBuilder *textCompare = [[CkoStringBuilder alloc] init];
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoJsonObject *config = [[CkoJsonObject alloc] init];
config.Strict = YES;
success = [config LoadFile: @"qa_data/azure/storage.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not load private Azure configuration.");
    return;
}

[textCompare Clear];
[textCompare SetString: [config StringOf: @"account"]];
BOOL sameText1 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"account"] intValue] != 1) || (sameText1 == YES)) {
    NSLog(@"%@",@"Supply the storage account name.");
    return;
}

CkoRest *identity = [[CkoRest alloc] init];
identity.ConnectTimeoutMs = [NSNumber numberWithInt:5000];
identity.IdleTimeoutMs = [NSNumber numberWithInt:30000];
CkoDateTime *expires = [[CkoDateTime alloc] init];
success = [expires SetFromCurrentSystemTime];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

[textCompare Clear];
[textCompare SetString: [config StringOf: @"tenant_id"]];
BOOL sameText2 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"tenant_id"] intValue] != 1) || (sameText2 == YES)) {
    NSLog(@"%@",@"Missing string tenant_id.");
    return;
}

[textCompare Clear];
[textCompare SetString: [config StringOf: @"client_id"]];
BOOL sameText3 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"client_id"] intValue] != 1) || (sameText3 == YES)) {
    NSLog(@"%@",@"Missing string client_id.");
    return;
}

[textCompare Clear];
[textCompare SetString: [config StringOf: @"client_secret"]];
BOOL sameText4 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"client_secret"] intValue] != 1) || (sameText4 == YES)) {
    NSLog(@"%@",@"Missing string client_secret.");
    return;
}

CkoStringBuilder *validate1 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet1 = [[CkoStringBuilder alloc] init];
success = [validate1 Append: [config StringOf: @"tenant_id"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [alphabet1 Append: @"0123456789abcdefABCDEF-"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

if (([validate1.Length intValue] < 36) || ([validate1.Length intValue] > 36)) {
    NSLog(@"%@",@"Invalid tenant GUID length.");
    return;
}

int index1 = 0;
while ((index1 < [validate1.Length intValue])) {
    if ([alphabet1 Contains: [validate1 GetRange: [NSNumber numberWithInt: index1] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid tenant GUID.");
        return;
    }

    index1 = index1 + 1;
}

CkoSocket *identityTls = [[CkoSocket alloc] init];
identityTls.RequireSslCertVerify = YES;
identityTls.MaxReadIdleMs = [NSNumber numberWithInt:30000];
identityTls.MaxSendIdleMs = [NSNumber numberWithInt:30000];
success = [identityTls Connect: @"login.microsoftonline.com" port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 20000]];
if (success != YES) {
    NSLog(@"%@",@"Identity TLS connection failed.");
    return;
}

success = [identity UseConnection: identityTls autoReconnect: NO];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

CkoStringBuilder *tokenPath = [[CkoStringBuilder alloc] init];
success = [tokenPath Append: @"/"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [tokenPath Append: [config StringOf: @"tenant_id"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [tokenPath Append: @"/oauth2/v2.0/token"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [identity AddQueryParam: @"grant_type" value: @"client_credentials"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [identity AddQueryParam: @"scope" value: @"https://storage.azure.com/.default"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [identity AddQueryParam: @"client_id" value: [config StringOf: @"client_id"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [identity AddQueryParam: @"client_secret" value: [config StringOf: @"client_secret"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

//  FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
NSString *tokenText = [identity FullRequestFormUrlEncoded: @"POST" uriPath: [tokenPath GetAsString]];
if (identity.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Token request did not complete.");
    return;
}

if ([identity.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@",@"Token request failed; keep its body and diagnostics private.");
    return;
}

CkoJsonObject *token = [[CkoJsonObject alloc] init];
token.Strict = YES;
success = [token Load: tokenText];
if (success != YES) {
    NSLog(@"%@",@"Expected token JSON.");
    return;
}

[textCompare Clear];
[textCompare SetString: [token StringOf: @"access_token"]];
BOOL sameText5 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || (sameText5 == YES)) {
    NSLog(@"%@",@"Missing string access_token.");
    return;
}

[textCompare Clear];
[textCompare SetString: [token StringOf: @"token_type"]];
BOOL sameText6 = [textCompare ContentsEqual: @"Bearer" caseSensitive: YES];
if (sameText6 != YES) {
    NSLog(@"%@",@"Expected a bearer token.");
    return;
}

if (([[token JsonTypeOf: @"expires_in"] intValue] != 2)) {
    NSLog(@"%@",@"Invalid token lifetime type.");
    return;
}

CkoStringBuilder *validate2 = [[CkoStringBuilder alloc] init];
CkoStringBuilder *alphabet2 = [[CkoStringBuilder alloc] init];
success = [validate2 Append: [token StringOf: @"expires_in"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [alphabet2 Append: @"0123456789"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

if (([validate2.Length intValue] < 1) || ([validate2.Length intValue] > 6)) {
    NSLog(@"%@",@"Invalid token lifetime length.");
    return;
}

int index2 = 0;
while ((index2 < [validate2.Length intValue])) {
    if ([alphabet2 Contains: [validate2 GetRange: [NSNumber numberWithInt: index2] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Invalid token lifetime.");
        return;
    }

    index2 = index2 + 1;
}

int seconds = [[token IntOf: @"expires_in"] intValue];
if ((seconds < 120) || (seconds > 86400)) {
    NSLog(@"%@",@"Unexpected token lifetime.");
    return;
}

success = [expires AddSeconds: [NSNumber numberWithInt: seconds]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

CkoDateTime *now = [[CkoDateTime alloc] init];
success = [now SetFromCurrentSystemTime];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

if ([[expires DiffSeconds: now] intValue] <= 60) {
    NSLog(@"%@",@"Token expired while the request was in progress.");
    return;
}

CkoJsonObject *saved = [[CkoJsonObject alloc] init];
success = [saved UpdateString: @"access_token" value: [token StringOf: @"access_token"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [saved UpdateString: @"expires_at" value: [expires GetAsTimestamp: NO]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [saved UpdateString: @"resource" value: @"https://storage.azure.com/"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [saved WriteFile: @"qa_data/azure/table-token.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not save the private token file.");
    return;
}

CkoStringBuilder *accountText = [[CkoStringBuilder alloc] init];
success = [accountText Append: [config StringOf: @"account"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

if (([accountText.Length intValue] < 3) || ([accountText.Length intValue] > 24)) {
    NSLog(@"%@",@"Storage account names need 3-24 lowercase letters or digits.");
    return;
}

CkoStringBuilder *allowedAccountChars = [[CkoStringBuilder alloc] init];
success = [allowedAccountChars Append: @"abcdefghijklmnopqrstuvwxyz0123456789"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

int charIndex = 0;
while ((charIndex < [accountText.Length intValue])) {
    if ([allowedAccountChars Contains: [accountText GetRange: [NSNumber numberWithInt: charIndex] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
        NSLog(@"%@",@"Storage account names need lowercase letters or digits.");
        return;
    }

    charIndex = charIndex + 1;
}

CkoStringBuilder *endpoint = [[CkoStringBuilder alloc] init];
success = [endpoint Append: [config StringOf: @"account"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [endpoint Append: @".table.core.windows.net"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

//  Table names contain only letters and digits and are case-insensitive.
//  Entity PartitionKey and RowKey values are case-sensitive.
NSString *resourcePath = @"/ExampleCustomers()";
CkoRest *rest = [[CkoRest alloc] init];
rest.ConnectTimeoutMs = [NSNumber numberWithInt:20000];
rest.IdleTimeoutMs = [NSNumber numberWithInt:60000];
CkoSocket *connection = [[CkoSocket alloc] init];
connection.RequireSslCertVerify = YES;
connection.MaxReadIdleMs = [NSNumber numberWithInt:60000];
connection.MaxSendIdleMs = [NSNumber numberWithInt:60000];
success = [connection Connect: [endpoint GetAsString] port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 20000]];
if (success != YES) {
    NSLog(@"%@",@"TLS connection failed; inspect diagnostics privately.");
    return;
}

//  Do not replay a write automatically after losing its response.
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

NSString *responseText = 0;
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.Strict = YES;
success = [rest AddHeader: @"DataServiceVersion" value: @"3.0;NetFx"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [rest AddHeader: @"MaxDataServiceVersion" value: @"3.0;NetFx"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [rest AddHeader: @"Content-Type" value: @"application/json"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [rest AddHeader: @"Accept" value: @"application/json;odata=minimalmetadata"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [rest AddHeader: @"x-ms-version" value: @"2026-04-06"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

CkoStringBuilder *authorization = [[CkoStringBuilder alloc] init];
success = [authorization Append: @"Bearer "];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [authorization Append: [token StringOf: @"access_token"]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [rest AddHeader: @"Authorization" value: [authorization GetAsString]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

NSString *partitionKey = @"customers-west";
NSString *rowKey = @"customer-001";
CkoStringBuilder *encodedPk = [[CkoStringBuilder alloc] init];
success = [encodedPk Append: partitionKey];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

int escapedencodedPk = [[encodedPk Replace: @"'" replacement: @"''"] intValue];
success = [encodedPk Encode: @"url_rfc3986" charset: @"utf-8"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

CkoStringBuilder *encodedRk = [[CkoStringBuilder alloc] init];
success = [encodedRk Append: rowKey];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

int escapedencodedRk = [[encodedRk Replace: @"'" replacement: @"''"] intValue];
success = [encodedRk Encode: @"url_rfc3986" charset: @"utf-8"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

//  Escape apostrophes for OData first, then percent-encode each key once.
CkoStringBuilder *entityPath = [[CkoStringBuilder alloc] init];
success = [entityPath Append: @"/ExampleCustomers(PartitionKey='"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [entityPath Append: [encodedPk GetAsString]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [entityPath Append: @"',RowKey='"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [entityPath Append: [encodedRk GetAsString]];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

success = [entityPath Append: @"')"];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

resourcePath = [entityPath GetAsString];
success = [now SetFromCurrentSystemTime];
if (success != YES) {
    NSLog(@"%@",@"Could not prepare the request.");
    return;
}

if ([[expires DiffSeconds: now] intValue] <= 60) {
    NSLog(@"%@",@"Reacquire a token before the Table request.");
    return;
}

responseText = [rest FullRequestNoBody: @"GET" uriPath: resourcePath];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Request did not complete; inspect the target before retrying a write.");
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"HTTP status: ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@%@",@"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.
    return;
}

success = [result Load: responseText];
if (success != YES) {
    NSLog(@"%@",@"Expected well-formed JSON.");
    return;
}

[textCompare Clear];
[textCompare SetString: [result StringOf: @"PartitionKey"]];
BOOL sameText7 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[result JsonTypeOf: @"PartitionKey"] intValue] != 1) || (sameText7 == YES)) {
    NSLog(@"%@",@"Missing string PartitionKey.");
    return;
}

[textCompare Clear];
[textCompare SetString: [result StringOf: @"RowKey"]];
BOOL sameText8 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[result JsonTypeOf: @"RowKey"] intValue] != 1) || (sameText8 == YES)) {
    NSLog(@"%@",@"Missing string RowKey.");
    return;
}

[textCompare Clear];
[textCompare SetString: [result StringOf: @"PartitionKey"]];
BOOL sameText9 = [textCompare ContentsEqual: partitionKey caseSensitive: YES];
[textCompare Clear];
[textCompare SetString: [result StringOf: @"RowKey"]];
BOOL sameText10 = [textCompare ContentsEqual: rowKey caseSensitive: YES];
if ((sameText9 != YES) || (sameText10 != YES)) {
    NSLog(@"%@",@"Returned entity keys do not match.");
    return;
}

NSLog(@"%@",@"Storage token obtained and Table entity retrieved.");