Forum Discussion
MS Fabric - Bearer Token retreaving
Hello,
Could someone explain me if is it possible to retreave Bearer token in MS Fabric notebook ? I am trying to download data from MS Planner via "https://graph.microsoft.com/v1.0/planner/plans/YOUR_PLAN_ID/tasks" but there is important to have token for authentification.
Everything is in the same tenant and the plan is also mine so i am just trying to retrive my plan via my notebook in fabric. I can do that with PowerBi desktop but its easier for me to retrieve the data via JSON and create dim and fact tables for DirectLake connection.
I know about APP registration in AZURE and etc but when i am logged in, it could be working just from notebook.
Here is a sample of my code without token:
from notebookutils import mssparkutils
import requests
# Retrieve the Azure AD token using mssparkutils
scope = "https://graph.microsoft.com/.default"
token = mssparkutils.credentials.getToken(scope)
# It is failing here because there is no option for scope but just pbi, KQL and etc.
# Use the token in the Authorization header
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"
response = requests.get(url, headers=headers)
# Handle the response
if response.status_code == 200:
print("Planner Plan Details:", response.json())
else:
print(f"Error {response.status_code}: {response.text}")
Thank you very much for any help.
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.
2 Replies
- nilendraFabricSuper User
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 - v-ssriganeshCommunity Support
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.