Sample code for 30+ languages & platforms
C

Configure Azure SAS Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthAzureSas, which associates an AuthAzureSAS provider so requests are authorized with an Azure Shared Access Signature token.

Background. A SAS token grants time-limited, permission-scoped access to Azure Storage resources without exposing the account key on the wire. Use SAS authorization when a query-token approach is preferred over Shared Key headers.

Chilkat C Downloads

C
#include <C_CkRest.h>
#include <C_CkAuthAzureSAS.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkAuthAzureSAS azSas;
    const char *responseText;

    success = FALSE;

    rest = CkRest_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    //  Configure Azure Shared Access Signature (SAS) authentication.  Provide the account access key
    //  and the SAS token query parameters; Chilkat adds the SAS token to each request.
    azSas = CkAuthAzureSAS_Create();
    //  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    //  interactive prompt, environment variable, or a secrets vault.
    CkAuthAzureSAS_putAccessKey(azSas,"BASE64_ACCOUNT_ACCESS_KEY");

    //  Associate the provider with the REST object.
    success = CkRest_SetAuthAzureSas(rest,azSas);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAzureSAS_Dispose(azSas);
        return;
    }

    responseText = CkRest_fullRequestNoBody(rest,"GET","/mycontainer/myblob.txt");
    if (CkRest_getLastMethodSuccess(rest) == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAzureSAS_Dispose(azSas);
        return;
    }

    printf("%s\n",responseText);


    CkRest_Dispose(rest);
    CkAuthAzureSAS_Dispose(azSas);

    }