Step 1: Create an Azure Active Directory (AAD) Application
First, you need to register an application in Azure Active Directory. This will allow you to authenticate with Dynamics 365 Business Central.
- Sign in to the Azure portal: Go to Azure Portal.
- Register an application:
- Navigate to Azure Active Directory > App registrations > New registration.
- Enter a Name for your application (e.g.,
BusinessCentralAPI
). - Under Supported account types, choose Accounts in this organizational directory only.
- Click Register.
- Record the Application (client) ID and Directory (tenant) ID. You’ll need these later.
- Create a client secret:
- Go to Certificates & secrets.
- Click on New client secret.
- Add a description and set the expiration period.
- Record the Value of the client secret. This is your password.
Step 2: Assign API Permissions
You need to assign permissions to your AAD application to access Business Central data.
- Navigate to API permissions:
- In your registered app, click on API permissions.
- Click Add a permission.
- Select APIs my organization uses:
- Search for
Dynamics 365 Business Central
. - Choose Delegated permissions.
- Select permissions like API.ReadWrite.All.
- Click Add permissions.
- Search for
- Grant admin consent for your organization:
- Click Grant admin consent and confirm.
Step 3: Set Up Permissions in Business Central
Next, set up permissions in Business Central to allow the application to access the data.
- Go to Business Central and sign in as an admin.
- Navigate to User Settings:
- Go to Users and select your user.
- Assign the necessary permission sets to your user.
- Assign the AAD application:
- Go to Azure Active Directory Applications.
- Select your app and assign permission sets for the app.
Step 4: Generate a Web Access Key
Now, you’ll generate a web access key using the AAD credentials.
- Base64 Encoding:
- You need to create a base64-encoded string from your
client_id
,client_secret
, andtenant_id
.
import base64 client_id = 'your-client-id' client_secret = 'your-client-secret' tenant_id = 'your-tenant-id' key = f'{client_id}:{client_secret}:{tenant_id}' web_access_key = base64.b64encode(key.encode()).decode() print("Web Access Key:", web_access_key)
Replaceyour-client-id
,your-client-secret
, andyour-tenant-id
with your actual values. - You need to create a base64-encoded string from your
- Using the Web Access Key:
- Use the generated key as the Authorization header when making API calls.
- Example:makefileCopy code
Authorization: Bearer <web_access_key>
Step 5: Access the API Endpoint
Use the generated web access key to make requests to the Business Central API.
Here’s an example using Python to access the specified endpoint:
pythonCopy codeimport requests
# API endpoint
url = "https://api.businesscentral.dynamics.com/v2.0/9dd8b83b-d50c-4c3d-8dca-11e1c6bca830/Sandbox/ODataV4/Company('CRONUS IN')/Chart_of_Accounts"
# Headers
headers = {
"Authorization": "Bearer <web_access_key>",
"Content-Type": "application/json",
"Accept": "application/json"
}
# Make the GET request
response = requests.get(url, headers=headers)
# Print the response
print(response.json())
Troubleshooting
- Ensure all permissions are correctly set and consent is granted.
- Check that your Azure AD app registration is correctly configured.
- Verify that the API endpoint URL is correct and that you have access to the Business Central environment.
Additional Resources
Following these steps should allow you to generate a web access key and access the Microsoft Dynamics 365 Business Central API. Let me know if you need any further assistance!