Access Dataverse Web API with Server-to-Server connection
Hi,
I am trying to read data from certain tables in MS Dataverse using the Dynamics CRM API. I completed the following steps:
- Register application in Microsoft Entra ID
- Allow API permissions for Dynamics CRM API
- Grant admin consent for the API permissions
- Add Application User in Power Platform Admin Console
- I assigned the role "System Administrator" to make sure I have sufficient priviliges, however I intend to create a custom role in the future if everything works
- Add registered application to the Application User
I am able to read data by authenticating with my own personal account using the Azure CLI. However, I need to be able to read the data by authenticating with the Application User (Server-to-Server). For the API call I do the folIowing:
-----Request access token by authenticating through client credentials:
DATAVERSE_URL = "https://<organisation>.crm4.dynamics.com"
url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
data = {
"client_id": <client_id>,
"client_secret": <client_secret>,
"scope": DATAVERSE_URL + "/.default",
"grant_type": "client_credentials",
}
-----Call Dataverse Web API:
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
api_url = DATAVERSE_URL + "/api/data/v9.2/accounts"
I keep getting the same error:
"The user is not a member of the organization."
I feel like I am missing a step here. I hope you can point me in the right direction.
Thanks in advance!