PowerBuilder
PowerBuilder
How to Generate an Azure Service Bus Shared Access Signature (SAS)
See more Azure Service Bus Examples
Demonstrates generating and using an Azure Service Bus Shared Access Signature (SAS).Note: This example requires Chilkat v9.5.0.65 or greater.
Chilkat PowerBuilder Downloads
integer li_rc
oleobject loo_AuthSas
oleobject loo_DtExpiry
oleobject loo_SbResourceUri
string ls_SasToken
oleobject loo_Fac
// Note: Requires Chilkat v9.5.0.65 or greater.
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// -------------------------------------------------------------------
// Create a Shared Access Signature (SAS) token for Azure Service Bus.
// -------------------------------------------------------------------
loo_AuthSas = create oleobject
li_rc = loo_AuthSas.ConnectToNewObject("Chilkat.AuthAzureSAS")
if li_rc < 0 then
destroy loo_AuthSas
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_AuthSas.AccessKey = "AzureServiceBus_PrimaryKey"
// The SAS token for Service Bus will look like this:
// (The order of params will be different. The order does not matter.)
// sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI>
// Specify the format of the string to sign.
loo_AuthSas.StringToSign = "resourceURI,expiry"
// Create an expiry to 30 days in the future.
loo_DtExpiry = create oleobject
li_rc = loo_DtExpiry.ConnectToNewObject("Chilkat.CkDateTime")
loo_DtExpiry.SetFromCurrentSystemTime()
loo_DtExpiry.AddDays(30)
loo_AuthSas.SetTokenParam("expiry","se",loo_DtExpiry.GetAsUnixTimeStr(1))
// Set the skn (keyname)
// This example uses the key "RootManageSharedAccessKey". This give full access.
// In a typical scenario, you would create a new Azure key (for the service bus)
// in the Azure portal, such that the key has limited permissions. This would
// allow you to give the SAS token to others for specific access for some period of time.
loo_AuthSas.SetTokenParam("keyName","skn","RootManageSharedAccessKey")
// Set the URL-encoded-resourceURI
loo_SbResourceUri = create oleobject
li_rc = loo_SbResourceUri.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbResourceUri.Append("https://<yournamespace>.servicebus.windows.net/")
loo_SbResourceUri.Encode("url","utf-8")
loo_AuthSas.SetTokenParam("resourceURI","sr",loo_SbResourceUri.GetAsString())
// Generate the SAS token.
ls_SasToken = loo_AuthSas.GenerateToken()
if loo_AuthSas.LastMethodSuccess <> 1 then
Write-Debug loo_AuthSas.LastErrorText
destroy loo_AuthSas
destroy loo_DtExpiry
destroy loo_SbResourceUri
return
end if
Write-Debug "SAS token: " + ls_SasToken
// Save the SAS token to a file.
// We can then use this pre-generated token for future Service Bus operations.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
loo_Fac.WriteEntireTextFile("qa_data/tokens/serviceBusSas.txt",ls_SasToken,"utf-8",0)
destroy loo_AuthSas
destroy loo_DtExpiry
destroy loo_SbResourceUri
destroy loo_Fac