Skip to content

Instantly share code, notes, and snippets.

@dvu4
Created November 16, 2022 15:21
Show Gist options
  • Save dvu4/766a68e15c0100e9eea419f12e04aa24 to your computer and use it in GitHub Desktop.
Save dvu4/766a68e15c0100e9eea419f12e04aa24 to your computer and use it in GitHub Desktop.
this script will retrieve the connection string for sending and receiving the message through Azure Servicebus
import requests
from azure.identity import DefaultAzureCredential
NAMESPACE_NAME = "namespace_name"
SUBSCRIPTION_ID = "subscription_id"
RESOURCE_GROUP = "resource-group"
AUTHORIZATION_RULE = "RootManageSharedAccessKey"
API_VERSION = "2021-11-01"
def get_servicebus_connection_string():
credential = DefaultAzureCredential()
token = credential.get_token("https://management.azure.com/.default")
url = f"https://management.azure.com/subscriptions/{SUBSCRIPTION_ID}" \
f"/resourceGroups/{RESOURCE_GROUP}/providers/Microsoft.ServiceBus/namespaces" \
f"/{NAMESPACE_NAME}/AuthorizationRules/{AUTHORIZATION_RULE}/listKeys?api-version={API_VERSION}"
auth_header = {'Authorization': 'Bearer ' + token.token, "Content-Type": "application/json"}
response = requests.post(url=url, headers=auth_header)
list_keys = response.json()
connection_string = list_keys["primaryConnectionString"]
return connection_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment