Dynamics 365 Web API giving 401 error?

时间:2018-08-22 13:47:31

标签: oauth-2.0 dynamics-crm microsoft-dynamics dynamics-365 dynamics-crm-webapi

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 个答案:

答案 0 :(得分:0)

您必须使用如下所示的组织URI :(已从代码段中删除.api.

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

我看到您已经初始化了一个名为api = 'https://xxxxxx.crm8.dynamics.com/api/data/v9.0/'的变量,该变量是正确的,但是您没有使用它。

答案 1 :(得分:0)

您是否已同意用户使用所需资源? 请参阅下面的链接,并在“请求授权码”下查找。 您将需要添加带有'consent'作为值的'prompt'参数。 然后,如果尚未授予同意,Azure AD将决定带您进入同意页面。

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