Skip to content

Instantly share code, notes, and snippets.

@ugoenyioha
Last active November 5, 2023 18:06
Show Gist options
  • Save ugoenyioha/33b1d3b207c5d2a58c2fa8c6526f2fa0 to your computer and use it in GitHub Desktop.
Save ugoenyioha/33b1d3b207c5d2a58c2fa8c6526f2fa0 to your computer and use it in GitHub Desktop.
Sample Code Snippets
// Connect to Azure with MSAL and Private Certs - Remember to Create an App
from msal import ConfidentialClientApplication
import jwt # You need PyJWT library, install using `pip install PyJWT`
# Load your certificate and get the thumbprint
with open("path/to/your/certificate.pem", 'r') as cert_file:
cert_str = cert_file.read()
# Extract the thumbprint and private key from the certificate
thumbprint = jwt.algorithms.RSAAlgorithm.from_jwk(cert_str).thumbprint().hex()
private_key = open("path/to/your/private/key.pem", 'r').read()
# Your app's registration information
client_id = "your-client-id"
authority = f"https://login.microsoftonline.com/your-tenant-id"
scope = ["scope1", "scope2"] # The scopes/permissions the app requires
# Create a confidential client application
client_app = ConfidentialClientApplication(
client_id=client_id,
authority=authority,
client_credential={"thumbprint": thumbprint, "private_key": private_key},
)
# Acquire a token (this example uses client credentials flow)
token_response = client_app.acquire_token_for_client(scopes=scope)
client = BlobServiceClient(
account_url=azure_blob_account_url, credential=token_response)
blob_client = client.get_blob_client(container=bucket, blob='test.txt')
blob_download = blob_client.download_blob()
blob_content = blob_download.readall().decode("utf-8")
print(token_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment