Forum Discussion
MS Fabric - Bearer Token retreaving
- 1 year ago
Hi SnackSeeker91,
Thank you posting your in Microsoft fabric community forum.The answer provided by @nilendraFabric was indeed correct. As mentioned, mssparkutils.credentials.getToken() in Microsoft Fabric notebooks does not support retrieving tokens for Microsoft Graph API directly. Instead, you’ll need to use Azure App Registration with the Client Credentials flow to get an access token, as demonstrated in the solution provided by @SuperUser.
Additional Points to Consider:
- Instead of hardcoding client_id and client_secret, store them securely in Azure Key Vault and retrieve them dynamically in your notebook.
- If you're using Managed Identity in your Fabric workspace (if supported in the future), you may authenticate directly without needing to store secrets.
- Please Ensure that your registered app in Azure AD has the correct permissions (Planner.ReadWrite.All for full access). If you face permission errors, an Azure AD admin may need to grant consent.
If this helps, then please Accept it as a solution and dropping a "Kudos" so other members can find it more easily.
Thank you.
hello SnackSeeker91
The mssparkutils.credentials.getToken() method in Microsoft Fabric notebooks is designed to retrieve Azure Active Directory (AAD) tokens for specific predefined scopes or audiences, such as Power BI (pbi), KQL (kusto), and others supported within the Fabric ecosystem. However, it does not natively support retrieving tokens for the Microsoft Graph API scope (https://graph.microsoft.com/.default) as per the official documentation and user reports.
You can use the requests library in Python to authenticate with Azure AD and retrieve the token.
import requests
# Azure AD details
tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
scope = "https://graph.microsoft.com/.default"
# URL for obtaining token
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
# Request body
data = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}
# Make the request
response = requests.post(token_url, data=data)
if response.status_code == 200:
token = response.json().get("access_token")
print("Token retrieved successfully!")
else:
print(f"Error {response.status_code}: {response.text}")
and then use this token
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# Example API call to Microsoft Graph
url = "https://graph.microsoft.com/v1.0/planner/plans/YOUR_PLAN_ID/tasks"
response = requests.get(url, headers=headers)
# Handle the response
if response.status_code == 200:
print("Planner Tasks:", response.json())
else:
print(f"Error {response.status_code}: {response.text}")
If this is helpful , please accept this solution.
Thanks