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

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilder textCompare = new CkStringBuilder();
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObject config = new CkJsonObject();
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/storage.json");
    if (success != true) {
        System.out.println("Could not load private Azure configuration.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("account"));
    boolean sameText1 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
        System.out.println("Supply the storage account name.");
        return;
        }

    CkRest identity = new CkRest();
    identity.put_ConnectTimeoutMs(5000);
    identity.put_IdleTimeoutMs(30000);
    CkDateTime expires = new CkDateTime();
    success = expires.SetFromCurrentSystemTime();
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("tenant_id"));
    boolean sameText2 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("tenant_id") != 1) || (sameText2 == true)) {
        System.out.println("Missing string tenant_id.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("client_id"));
    boolean sameText3 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("client_id") != 1) || (sameText3 == true)) {
        System.out.println("Missing string client_id.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("client_secret"));
    boolean sameText4 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("client_secret") != 1) || (sameText4 == true)) {
        System.out.println("Missing string client_secret.");
        return;
        }

    CkStringBuilder validate1 = new CkStringBuilder();
    CkStringBuilder alphabet1 = new CkStringBuilder();
    success = validate1.Append(config.stringOf("tenant_id"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = alphabet1.Append("0123456789abcdefABCDEF-");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if ((validate1.get_Length() < 36) || (validate1.get_Length() > 36)) {
        System.out.println("Invalid tenant GUID length.");
        return;
        }

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

        index1 = index1+1;
        }

    CkSocket identityTls = new CkSocket();
    identityTls.put_RequireSslCertVerify(true);
    identityTls.put_MaxReadIdleMs(30000);
    identityTls.put_MaxSendIdleMs(30000);
    success = identityTls.Connect("login.microsoftonline.com",443,true,20000);
    if (success != true) {
        System.out.println("Identity TLS connection failed.");
        return;
        }

    success = identity.UseConnection(identityTls,false);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    CkStringBuilder tokenPath = new CkStringBuilder();
    success = tokenPath.Append("/");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = tokenPath.Append(config.stringOf("tenant_id"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = tokenPath.Append("/oauth2/v2.0/token");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = identity.AddQueryParam("grant_type","client_credentials");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

    success = identity.AddQueryParam("client_id",config.stringOf("client_id"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = identity.AddQueryParam("client_secret",config.stringOf("client_secret"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    //  FullRequestFormUrlEncoded places the parameters in the POST body, not the URL.
    String tokenText = identity.fullRequestFormUrlEncoded("POST",tokenPath.getAsString());
    if (identity.get_LastMethodSuccess() != true) {
        System.out.println("Token request did not complete.");
        return;
        }

    if (identity.get_ResponseStatusCode() != 200) {
        System.out.println("Token request failed; keep its body and diagnostics private.");
        return;
        }

    CkJsonObject token = new CkJsonObject();
    token.put_Strict(true);
    success = token.Load(tokenText);
    if (success != true) {
        System.out.println("Expected token JSON.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(token.stringOf("access_token"));
    boolean sameText5 = textCompare.ContentsEqual("",true);
    if ((token.JsonTypeOf("access_token") != 1) || (sameText5 == true)) {
        System.out.println("Missing string access_token.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(token.stringOf("token_type"));
    boolean sameText6 = textCompare.ContentsEqual("Bearer",true);
    if (sameText6 != true) {
        System.out.println("Expected a bearer token.");
        return;
        }

    if ((token.JsonTypeOf("expires_in") != 2)) {
        System.out.println("Invalid token lifetime type.");
        return;
        }

    CkStringBuilder validate2 = new CkStringBuilder();
    CkStringBuilder alphabet2 = new CkStringBuilder();
    success = validate2.Append(token.stringOf("expires_in"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = alphabet2.Append("0123456789");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if ((validate2.get_Length() < 1) || (validate2.get_Length() > 6)) {
        System.out.println("Invalid token lifetime length.");
        return;
        }

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

        index2 = index2+1;
        }

    int seconds = token.IntOf("expires_in");
    if ((seconds < 120) || (seconds > 86400)) {
        System.out.println("Unexpected token lifetime.");
        return;
        }

    success = expires.AddSeconds(seconds);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    CkDateTime now = new CkDateTime();
    success = now.SetFromCurrentSystemTime();
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if (expires.DiffSeconds(now) <= 60) {
        System.out.println("Token expired while the request was in progress.");
        return;
        }

    CkJsonObject saved = new CkJsonObject();
    success = saved.UpdateString("access_token",token.stringOf("access_token"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = saved.UpdateString("expires_at",expires.getAsTimestamp(false));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

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

    CkStringBuilder accountText = new CkStringBuilder();
    success = accountText.Append(config.stringOf("account"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
        System.out.println("Storage account names need 3-24 lowercase letters or digits.");
        return;
        }

    CkStringBuilder allowedAccountChars = new CkStringBuilder();
    success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

        charIndex = charIndex+1;
        }

    CkStringBuilder endpoint = new CkStringBuilder();
    success = endpoint.Append(config.stringOf("account"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = endpoint.Append(".table.core.windows.net");
    if (success != true) {
        System.out.println("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.
    String resourcePath = "/ExampleCustomers()";
    CkRest rest = new CkRest();
    rest.put_ConnectTimeoutMs(20000);
    rest.put_IdleTimeoutMs(60000);
    CkSocket connection = new CkSocket();
    connection.put_RequireSslCertVerify(true);
    connection.put_MaxReadIdleMs(60000);
    connection.put_MaxSendIdleMs(60000);
    success = connection.Connect(endpoint.getAsString(),443,true,20000);
    if (success != true) {
        System.out.println("TLS connection failed; inspect diagnostics privately.");
        return;
        }

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

    String responseText;
    CkJsonObject result = new CkJsonObject();
    result.put_Strict(true);
    success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("Content-Type","application/json");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

    CkStringBuilder authorization = new CkStringBuilder();
    success = authorization.Append("Bearer ");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = authorization.Append(token.stringOf("access_token"));
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = rest.AddHeader("Authorization",authorization.getAsString());
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    String partitionKey = "customers-west";
    String rowKey = "customer-001";
    CkStringBuilder encodedPk = new CkStringBuilder();
    success = encodedPk.Append(partitionKey);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

    CkStringBuilder encodedRk = new CkStringBuilder();
    success = encodedRk.Append(rowKey);
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

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

    //  Escape apostrophes for OData first, then percent-encode each key once.
    CkStringBuilder entityPath = new CkStringBuilder();
    success = entityPath.Append("/ExampleCustomers(PartitionKey='");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append(encodedPk.getAsString());
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append("',RowKey='");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append(encodedRk.getAsString());
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    success = entityPath.Append("')");
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    resourcePath = entityPath.getAsString();
    success = now.SetFromCurrentSystemTime();
    if (success != true) {
        System.out.println("Could not prepare the request.");
        return;
        }

    if (expires.DiffSeconds(now) <= 60) {
        System.out.println("Reacquire a token before the Table request.");
        return;
        }

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

    if (rest.get_ResponseStatusCode() != 200) {
        System.out.println("HTTP status: " + rest.get_ResponseStatusCode());
        System.out.println("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 != true) {
        System.out.println("Expected well-formed JSON.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    boolean sameText7 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("PartitionKey") != 1) || (sameText7 == true)) {
        System.out.println("Missing string PartitionKey.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    boolean sameText8 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("RowKey") != 1) || (sameText8 == true)) {
        System.out.println("Missing string RowKey.");
        return;
        }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    boolean sameText9 = textCompare.ContentsEqual(partitionKey,true);
    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    boolean sameText10 = textCompare.ContentsEqual(rowKey,true);
    if ((sameText9 != true) || (sameText10 != true)) {
        System.out.println("Returned entity keys do not match.");
        return;
        }

    System.out.println("Storage token obtained and Table entity retrieved.");
  }
}