In case you don’t have Dynamics 365 yet, you can get it from here
This blog post will guide you through the process of connecting to Dataverse with Python. It will cover the steps to create a service principal for Dynamics 365, add the service principal to Dynamics 365, and then connect to Dataverse with Python. Additionally, the post will explain how to query data from Dataverse with Python and how to visualize the data with Streamlit. By following this guide, you will be able to easily connect to Dataverse and work with its data using Python.
1. Create service principal
Go to Azure
From search, search “Microsoft Entra ID”, then from the left side select “App registrations” and “New registrations.
After you have create to the service principal, go to “App permissions”. From the search for Dynamics CRM, this will add Dynamics as a service to the service principal.
Then Add permissions and admin consent.
Next step is to create client secret for you service principal, go to “Certificates & Secrets”.
The client id can be found from the App registration Overview “Applcations(client) ID”
NB! Remember to copy the secret, otherwise you need to create a new one.
Now you you have a service principal, client secret and you can go to Power platform admin center.
2. Power platform admin center
Admin platform can be found from here
Navigate to “Environments” select your environment, this demo is using a Sales Trial tenant. Then under settings you can find “Users + permissions”
Select “application users”, then “New App user”. Search for the create service principal under “App”, select the “business unit” and security role. In this demo we will use the system administrator.NB! In production always use custom roles. this will make sure that Service principal has the limited amount of access to resource in you power app environment. Table level rights are good practice to start with.
This is it, now you can use the service principal to interact with Odata.
2. Getting the bearer token
The secrets are located in streamlit secrets.toml
def get_token():
accesstoken = ''
try:
client_id = st.secrets["client_id"] ## step 1
client_secret = st.secrets["client_secret"] ## step 1
tenant_id = st.secrets["tenant_id"] ## can be found from Microsoft Entra ID
conn = http.client.HTTPSConnection("login.microsoftonline.com")
payload = 'grant_type=client_credentials&client_id={}&client_secret={}&scope=https://{}.crm4.dynamics.com/.default'.format(
client_id, client_secret, url)
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
getbearer = conn.request(
"GET", "/{}/oauth2/v2.0/token".format(tenant_id), payload, headers)
res = conn.getresponse()
data = res.read()
data_json = json.loads(data.decode("utf-8"))
accesstoken = data_json['access_token']
except KeyError:
print('Could not get access token')
If you run this code it will get you the bearer token to access odata.
3. Getting account data
There are several different ways to query data from dataverse, here is an example with selected columns:
def get_account():
# after select you can have whatever columns you want to query from dataverse
account = "https://{}.api.crm4.dynamics.com/api/data/v9.2/accounts?$select=description,name,revenue_base,creditlimit,address1_stateorprovince,openrevenue,creditlimit_base".format(
url)
accesstoken = get_token()
headers = {
'Authorization': 'Bearer ' + accesstoken,
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8'
}
response_data = json.loads(requests.get(
account, headers=headers).text)
df = pd.json_normalize(response_data['value'])
return df
4. Streamlit data
Streamlit provides a simple way to visualize data, so here are few kpis and a dataframe for data
Whole code can be found from here. The only thing you need to add is secrets in secrets.toml