1
votes

I was trying out Microsoft Dynamics 365 (v9.0) Web APis. I was able to generate access token but yet my api call is failing. I am using adal for python

from adal import AuthenticationContext
import requests

api = 'https://xxxxxx.crm8.dynamics.com/api/data/v9.0/'

auth_context = AuthenticationContext('https://login.microsoftonline.com/xxxxxx.com')


token = auth_context.acquire_token_with_client_credentials(
    resource='https://xxxxxx.com/0d74ff2f-d7d2-46f4-a3c3-fb4f6d49aa51',
    client_id='6e859f06-0d88-413f-a3fe-1ae12cc7350d',
    client_secret='abigclientsecret')

print(token)

at = 'Bearer ' + token['accessToken']

headers = {'Authorization' : at,
"Content-Type" : "application/json; charset=utf-8",
'OData-MaxVersion' :'4.0',
'OData-Version' :'4.0',
'Accept' : 'application/json'}
response = requests.get("https://xxxxxx.api.crm8.dynamics.com/api/data/v9.0/contacts", headers=headers)
print(response)
2

2 Answers

0
votes

You have to use Organization URI like below: (removed .api. from your snippet)

response = requests.get("https://xxxxxx.crm8.dynamics.com/api/data/v9.0/contacts", headers=headers)

I see you have initialized a variable called api = 'https://xxxxxx.crm8.dynamics.com/api/data/v9.0/' which is correct but you have not used it.

0
votes

Have you given consent for the user over the required resource? Refer to the link below and look under "Request an authorization code". You will need to add the 'prompt' parameter with 'consent' as the value. Azure AD will then decide to take you to a consent page if consent has not yet been granted.

https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-protocols-oauth-code