The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
How to authenticate to cosmos db using spn in fabric notebook or workspace identity in copy activity
Solved! Go to Solution.
this one didn't work for me, below code has worked
Hi @SravyaNeela ,
To authenticate to Cosmos DB from Fabric using either a Service Principal (SPN) or Workspace Managed Identity, here’s what you can do:
In your Fabric Notebook (PySpark or Python), use the following pattern:
from azure.cosmos import CosmosClient url = "<your-cosmos-db-uri>" key = "<your-client-secret>" # or use a credential object if using MSAL client_id = "<your-client-id>" tenant_id = "<your-tenant-id>" # Use MSAL to get token from msal import ConfidentialClientApplication app = ConfidentialClientApplication( client_id, authority=f"https://login.microsoftonline.com/{tenant_id}", client_credential=key ) token = app.acquire_token_for_client(scopes=["https://cosmos.azure.com/.default"]) headers = {"Authorization": f"Bearer {token['access_token']}"}
Then use this token to connect to Cosmos DB REST API or SDK.
If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.
this one didn't work for me, below code has worked
Hi @SravyaNeela,
Thank you for reaching out to Microsoft Fabric Community.
Thank you for the response and confirming that the above code worked. Since the issue is resolved, I request you to please mark the post as "Accept as Solution" so that other community members who has a similar issue will find it more quickly.
Thanks and regards,
Anjan Kumar Chippa
User | Count |
---|---|
20 | |
15 | |
6 | |
3 | |
3 |
User | Count |
---|---|
52 | |
28 | |
11 | |
9 | |
9 |