Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Microsoft Dataverse
Unanswered

Access Dynamics CRM API with Service Principal

Like (1) ShareShare
ReportReport
Posted on 12 Sep 2024 12:21:49 by 2
Hello there,
 
I am trying to read data from a couple of tables stored in Microsoft Dataverse through the API. I've completed the following steps:
  • Register application under App registrations in Microsoft Entra ID
  • Allow access to "Dynamics CRM" under API Permissions, also made sure to select Grant admin consent for XXX
  • Create new Application User in Power Platform Admin console (as described here: https://learn.microsoft.com/en-us/power-platform/admin/manage-application-users)
  • Added the newly registered application in Entra ID to the Application User
    • I added "System Administrator" Security Role for now, but intend to change this in the future. I only need to be able to read data, but I want to avoid having issues due to the insufficient priviliges
When I try to send a request to the api endpoint https://<organization>.crm4.dynamics.com/api/data/v9.2/<table> (url is directly from the power platform admin console) I keep getting the error 
 
... "The user is not a member of the organization."}}
 
I am able to successfully complete the request when I authenticate using Azure CLI, however the error occurs when I try to authenticate with client ID, client secret & tenant ID (as received from the MS Entra ID app registration). 
 
Code snippet (in Python) for performing the request:
 
---
import requests
from azure.identity import DefaultAzureCredential
 
DATAVERSE_URL = "https://<organization>.crm4.dynamics.com"
def get_token():
    credential = DefaultAzureCredential()
    token = credential.get_token(DATAVERSE_URL + '/.default')
    return token.token
 
def send_request():
    token = get_token()
    headers = {
        'Authorization': f'Bearer {token}',
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0',
        'Accept': 'application/json',
    }
   
    url = f"{DATAVERSE_URL}/api/data/v9.2/<table>"
    response = requests.get(url, headers=headers)
    return response
---
 
I am hoping you can pinpoint what I am doing wrong here.
 
Thanks in advance.  
  • Shashank Bhide Profile Picture
    936 Super User 2025 Season 1 on 13 Sep 2024 at 05:20:21
    Access Dynamics CRM API with Service Principal
    DefaultAzureCredentials class follows the below chain (in same order) to get a token and stops when one of the classes supplies the token, which one have you configured? I guess none, so create a managed identity (MI) and then give that MI rights in Dataverse like you did, and then use "ManagedIdentityCredential" or "DefaultAzureCredentials" to get the token, then you need to add the token to the request (like yo did) and then send it.
     
     
     
  • bscarlavai33 Profile Picture
    588 Super User 2025 Season 1 on 12 Sep 2024 at 21:54:14
    Access Dynamics CRM API with Service Principal
    The error seems like that app user isn't a part of the environment or doesn't have a role. Did you verify you added the app user to the correct environment when using the Admin Portal?
  • Suggested answer
    MichaelFP Profile Picture
    1,831 Super User 2025 Season 1 on 12 Sep 2024 at 13:37:40
    Access Dynamics CRM API with Service Principal
    Hi!
     
    You must acquire the token login in the tenant before start to use the Dataverse web api. You can do that using OAuth flow
     
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Microsoft Dataverse

#1
stampcoin Profile Picture

stampcoin 15

#2
ankit_singhal Profile Picture

ankit_singhal 11 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 9 Super User 2025 Season 1

Overall leaderboard

Featured topics

Loading started