Forum Discussion
Microsoft Dynamic 365 business Central API data fetch using Fabric note book
My company’s IT team has built APIs to access Microsoft Dynamics 365 Business Central data. I successfully tested these APIs using Postman. However, when I try to call the API to obtain the bearer access token from a Python script in a Microsoft Fabric notebook, it does not work. Below is the Python script I am using. I do not want to use data flows for this. Please note that this API fetches data only for a specific Azure AD account.
If this script isn’t the best approach, I’m open to trying alternative scripting methods
import requests
from urllib.parse import urlencode, urlparse, parse_qs
import webbrowser
tenant_id = "tenant_id"
client_id = "client_id"
client_secret = "client_secret"
redirect_uri = "https://oauth.powerbi.com/views/oauthredirect.html"
scope = "https://api.businesscentral.dynamics.com/.default"
# === OAuth Endpoints ===
auth_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize"
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
# === Direct to login page ===
params = {
"client_id": client_id,
"response_type": "code",
"redirect_uri": redirect_uri,
"response_mode": "query",
"scope": scope,
}
full_auth_url = f"{auth_url}?{urlencode(params)}"
webbrowser.open(full_auth_url)
print("Please login and paste the full redirect URL below.")
# === Capture the code from the redirect ===
redirect_response = input("Paste the full redirect URL here: ")
code = parse_qs(urlparse(redirect_response).query).get("code", [None])[0]
# === Exchange code for token ===
token_data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri,
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}
token_response = requests.post(token_url, data=token_data).json()
# === Get Access Token ===
if "access_token" in token_response:
access_token = token_response["access_token"]
print("\n Access Token:\n", access_token)
else:
print("\n Failed to acquire token:\n", token_response)
Hi kasun_rumesh ,
The Client Credentials Flow is ideal for non-interactive scenarios like Fabric notebooks. It allows your script to get an access token using just the Tenant ID, Client ID, and Client Secret, without needing user login. This is done by calling Azure AD's token endpoint with the appropriate parameters. Make sure your Azure AD app has the necessary application-level API permissions (e.g., Financials.ReadWrite.All) and that admin consent has been granted. Once the token is received, you can use it to call the Business Central API.
Thank you.
6 Replies
- v-venuppuCommunity Support
Hi kasun_rumesh ,
Thank you for reaching out to Microsoft Fabric Community.
Instead of using the interactive OAuth flow, switch to the Client Credentials Flow for automated API access. This method doesn’t require user interaction and allows you to authenticate using just your Client ID, Client Secret, and Tenant ID. The script will request an access token from Azure AD, which can then be used in API calls to Business Central. This is ideal for non-interactive scenarios like in Fabric notebooks.
Thank you.
- kasun_rumeshHelper II
I'm bit new to this. Can you bit further explain about the Client Credentials Flow
- v-venuppuCommunity Support
Hi kasun_rumesh ,
The Client Credentials Flow is ideal for non-interactive scenarios like Fabric notebooks. It allows your script to get an access token using just the Tenant ID, Client ID, and Client Secret, without needing user login. This is done by calling Azure AD's token endpoint with the appropriate parameters. Make sure your Azure AD app has the necessary application-level API permissions (e.g., Financials.ReadWrite.All) and that admin consent has been granted. Once the token is received, you can use it to call the Business Central API.
Thank you.
- v-venuppuCommunity Support
Hi kasun_rumesh ,
I wanted to check if you had the opportunity to review the information provided and resolve the issue..?Please let us know if you need any further assistance.We are happy to help.
Thank you.
- v-venuppuCommunity Support
Hi kasun_rumesh ,
May I ask if you have resolved this issue? Please let us know if you have any further issues, we are happy to help.
Thank you.
- v-venuppuCommunity Support
Hi kasun_rumesh ,
I hope the information provided is helpful.I wanted to check whether you were able to resolve the issue with the provided solutions.Please let us know if you need any further assistance.
Thank you.