Skip to main content

Notifications

Power Platform Community / Forums / Microsoft Dataverse / Access Dynamics CRM AP...
Microsoft Dataverse
Unanswered

Access Dynamics CRM API with Service Principal

Posted on 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
    Shashank Bhide 758 on at
    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
    bscarlavai33 196 on at
    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
    MichaelFP 1,063 on at
    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
     
     
     

Helpful resources

Quick Links

Welcome to the Power Platform…

We are thrilled to unveil the newly-launched Power Platform Communities!…

Community Update Sept 16…

Power Platform Community Update…

Welcome to the new Power Platform Community!…

We are excited to announce our new Copilot Cookbook Gallery in the Community…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 140,719

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,355

Leaderboard

Featured topics