Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAzureSAS.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  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.i = CkAuthAzureSAS::ckCreate()
    If azSas.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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::setCkAccessKey(azSas, "BASE64_ACCOUNT_ACCESS_KEY")

    ;  Associate the provider with the REST object.
    success = CkRest::ckSetAuthAzureSas(rest,azSas)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAzureSAS::ckDispose(azSas)
        ProcedureReturn
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/mycontainer/myblob.txt")
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAzureSAS::ckDispose(azSas)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)
    CkAuthAzureSAS::ckDispose(azSas)


    ProcedureReturn
EndProcedure