Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
kasun_rumesh
Helper I
Helper I

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)

 

4 REPLIES 4
v-venuppu
Community Support
Community 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-venuppu
Community Support
Community 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.

 

 

 

I'm bit new to this. Can you bit further explain about the  Client Credentials Flow

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.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.