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

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkDateTimeW.h>
#include <CkSocketW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilderW textCompare;
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObjectW config;
    config.put_Strict(true);
    success = config.LoadFile(L"qa_data/azure/storage.json");
    if (success != true) {
        wprintf(L"Could not load private Azure configuration.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf(L"account"));
    bool sameText1 = textCompare.ContentsEqual(L"",true);
    if ((config.JsonTypeOf(L"account") != 1) || (sameText1 == true)) {
        wprintf(L"Supply the storage account name.\n");
        return;
    }

    CkRestW identity;
    identity.put_ConnectTimeoutMs(5000);
    identity.put_IdleTimeoutMs(30000);
    CkDateTimeW expires;
    success = expires.SetFromCurrentSystemTime();
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf(L"tenant_id"));
    bool sameText2 = textCompare.ContentsEqual(L"",true);
    if ((config.JsonTypeOf(L"tenant_id") != 1) || (sameText2 == true)) {
        wprintf(L"Missing string tenant_id.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf(L"client_id"));
    bool sameText3 = textCompare.ContentsEqual(L"",true);
    if ((config.JsonTypeOf(L"client_id") != 1) || (sameText3 == true)) {
        wprintf(L"Missing string client_id.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf(L"client_secret"));
    bool sameText4 = textCompare.ContentsEqual(L"",true);
    if ((config.JsonTypeOf(L"client_secret") != 1) || (sameText4 == true)) {
        wprintf(L"Missing string client_secret.\n");
        return;
    }

    CkStringBuilderW validate1;
    CkStringBuilderW alphabet1;
    success = validate1.Append(config.stringOf(L"tenant_id"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = alphabet1.Append(L"0123456789abcdefABCDEF-");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    if ((validate1.get_Length() < 36) || (validate1.get_Length() > 36)) {
        wprintf(L"Invalid tenant GUID length.\n");
        return;
    }

    int index1 = 0;
    while ((index1 < validate1.get_Length())) {
        if (alphabet1.Contains(validate1.getRange(index1,1,false),true) != true) {
            wprintf(L"Invalid tenant GUID.\n");
            return;
        }

        index1 = index1 + 1;
    }

    CkSocketW identityTls;
    identityTls.put_RequireSslCertVerify(true);
    identityTls.put_MaxReadIdleMs(30000);
    identityTls.put_MaxSendIdleMs(30000);
    success = identityTls.Connect(L"login.microsoftonline.com",443,true,20000);
    if (success != true) {
        wprintf(L"Identity TLS connection failed.\n");
        return;
    }

    success = identity.UseConnection(identityTls,false);
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    CkStringBuilderW tokenPath;
    success = tokenPath.Append(L"/");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = tokenPath.Append(config.stringOf(L"tenant_id"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = tokenPath.Append(L"/oauth2/v2.0/token");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = identity.AddQueryParam(L"grant_type",L"client_credentials");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = identity.AddQueryParam(L"scope",L"https://storage.azure.com/.default");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = identity.AddQueryParam(L"client_id",config.stringOf(L"client_id"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = identity.AddQueryParam(L"client_secret",config.stringOf(L"client_secret"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    //  FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
    const wchar_t *tokenText = identity.fullRequestFormUrlEncoded(L"POST",tokenPath.getAsString());
    if (identity.get_LastMethodSuccess() != true) {
        wprintf(L"Token request did not complete.\n");
        return;
    }

    if (identity.get_ResponseStatusCode() != 200) {
        wprintf(L"Token request failed; keep its body and diagnostics private.\n");
        return;
    }

    CkJsonObjectW token;
    token.put_Strict(true);
    success = token.Load(tokenText);
    if (success != true) {
        wprintf(L"Expected token JSON.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(token.stringOf(L"access_token"));
    bool sameText5 = textCompare.ContentsEqual(L"",true);
    if ((token.JsonTypeOf(L"access_token") != 1) || (sameText5 == true)) {
        wprintf(L"Missing string access_token.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(token.stringOf(L"token_type"));
    bool sameText6 = textCompare.ContentsEqual(L"Bearer",true);
    if (sameText6 != true) {
        wprintf(L"Expected a bearer token.\n");
        return;
    }

    if ((token.JsonTypeOf(L"expires_in") != 2)) {
        wprintf(L"Invalid token lifetime type.\n");
        return;
    }

    CkStringBuilderW validate2;
    CkStringBuilderW alphabet2;
    success = validate2.Append(token.stringOf(L"expires_in"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = alphabet2.Append(L"0123456789");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    if ((validate2.get_Length() < 1) || (validate2.get_Length() > 6)) {
        wprintf(L"Invalid token lifetime length.\n");
        return;
    }

    int index2 = 0;
    while ((index2 < validate2.get_Length())) {
        if (alphabet2.Contains(validate2.getRange(index2,1,false),true) != true) {
            wprintf(L"Invalid token lifetime.\n");
            return;
        }

        index2 = index2 + 1;
    }

    int seconds = token.IntOf(L"expires_in");
    if ((seconds < 120) || (seconds > 86400)) {
        wprintf(L"Unexpected token lifetime.\n");
        return;
    }

    success = expires.AddSeconds(seconds);
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    CkDateTimeW now;
    success = now.SetFromCurrentSystemTime();
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    if (expires.DiffSeconds(now) <= 60) {
        wprintf(L"Token expired while the request was in progress.\n");
        return;
    }

    CkJsonObjectW saved;
    success = saved.UpdateString(L"access_token",token.stringOf(L"access_token"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = saved.UpdateString(L"expires_at",expires.getAsTimestamp(false));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = saved.UpdateString(L"resource",L"https://storage.azure.com/");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = saved.WriteFile(L"qa_data/azure/table-token.json");
    if (success != true) {
        wprintf(L"Could not save the private token file.\n");
        return;
    }

    CkStringBuilderW accountText;
    success = accountText.Append(config.stringOf(L"account"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
        wprintf(L"Storage account names need 3-24 lowercase letters or digits.\n");
        return;
    }

    CkStringBuilderW allowedAccountChars;
    success = allowedAccountChars.Append(L"abcdefghijklmnopqrstuvwxyz0123456789");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    int charIndex = 0;
    while ((charIndex < accountText.get_Length())) {
        if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
            wprintf(L"Storage account names need lowercase letters or digits.\n");
            return;
        }

        charIndex = charIndex + 1;
    }

    CkStringBuilderW endpoint;
    success = endpoint.Append(config.stringOf(L"account"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = endpoint.Append(L".table.core.windows.net");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    //  Table names contain only letters and digits and are case-insensitive.
    //  Entity PartitionKey and RowKey values are case-sensitive.
    const wchar_t *resourcePath = L"/ExampleCustomers()";
    CkRestW rest;
    rest.put_ConnectTimeoutMs(20000);
    rest.put_IdleTimeoutMs(60000);
    CkSocketW connection;
    connection.put_RequireSslCertVerify(true);
    connection.put_MaxReadIdleMs(60000);
    connection.put_MaxSendIdleMs(60000);
    success = connection.Connect(endpoint.getAsString(),443,true,20000);
    if (success != true) {
        wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
        return;
    }

    //  Do not replay a write automatically after losing its response.
    success = rest.UseConnection(connection,false);
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    const wchar_t *responseText = 0;
    CkJsonObjectW result;
    result.put_Strict(true);
    success = rest.AddHeader(L"DataServiceVersion",L"3.0;NetFx");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = rest.AddHeader(L"MaxDataServiceVersion",L"3.0;NetFx");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = rest.AddHeader(L"Content-Type",L"application/json");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = rest.AddHeader(L"Accept",L"application/json;odata=minimalmetadata");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = rest.AddHeader(L"x-ms-version",L"2026-04-06");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    CkStringBuilderW authorization;
    success = authorization.Append(L"Bearer ");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = authorization.Append(token.stringOf(L"access_token"));
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = rest.AddHeader(L"Authorization",authorization.getAsString());
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    const wchar_t *partitionKey = L"customers-west";
    const wchar_t *rowKey = L"customer-001";
    CkStringBuilderW encodedPk;
    success = encodedPk.Append(partitionKey);
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    int escapedencodedPk = encodedPk.Replace(L"'",L"''");
    success = encodedPk.Encode(L"url_rfc3986",L"utf-8");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    CkStringBuilderW encodedRk;
    success = encodedRk.Append(rowKey);
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    int escapedencodedRk = encodedRk.Replace(L"'",L"''");
    success = encodedRk.Encode(L"url_rfc3986",L"utf-8");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    //  Escape apostrophes for OData first, then percent-encode each key once.
    CkStringBuilderW entityPath;
    success = entityPath.Append(L"/ExampleCustomers(PartitionKey='");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = entityPath.Append(encodedPk.getAsString());
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = entityPath.Append(L"',RowKey='");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = entityPath.Append(encodedRk.getAsString());
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    success = entityPath.Append(L"')");
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    resourcePath = entityPath.getAsString();
    success = now.SetFromCurrentSystemTime();
    if (success != true) {
        wprintf(L"Could not prepare the request.\n");
        return;
    }

    if (expires.DiffSeconds(now) <= 60) {
        wprintf(L"Reacquire a token before the Table request.\n");
        return;
    }

    responseText = rest.fullRequestNoBody(L"GET",resourcePath);
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"Request did not complete; inspect the target before retrying a write.\n");
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"HTTP status: %d\n",rest.get_ResponseStatusCode());
        wprintf(L"Request ID: %s\n",rest.responseHdrByName(L"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 != true) {
        wprintf(L"Expected well-formed JSON.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf(L"PartitionKey"));
    bool sameText7 = textCompare.ContentsEqual(L"",true);
    if ((result.JsonTypeOf(L"PartitionKey") != 1) || (sameText7 == true)) {
        wprintf(L"Missing string PartitionKey.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf(L"RowKey"));
    bool sameText8 = textCompare.ContentsEqual(L"",true);
    if ((result.JsonTypeOf(L"RowKey") != 1) || (sameText8 == true)) {
        wprintf(L"Missing string RowKey.\n");
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf(L"PartitionKey"));
    bool sameText9 = textCompare.ContentsEqual(partitionKey,true);
    textCompare.Clear();
    textCompare.SetString(result.stringOf(L"RowKey"));
    bool sameText10 = textCompare.ContentsEqual(rowKey,true);
    if ((sameText9 != true) || (sameText10 != true)) {
        wprintf(L"Returned entity keys do not match.\n");
        return;
    }

    wprintf(L"Storage token obtained and Table entity retrieved.\n");
    }