ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 使用MS Dynamics CRM 2016的REST Web API在线进行身份验证

2019-05-23 08:53:28  阅读:288  来源: 互联网

标签:python rest azure-active-directory adal dynamics-crm-2016


我正在尝试访问新的REST API,以构建服务器到服务器接口,以将CRM与其他应用程序(如网店等)集成.

我尝试了从Azure AD获取访问令牌的两种方法:

客户凭证

import adal

token_response = adal.acquire_token_with_client_credentials(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff', 
    client_id,
    secret
)

和用户/密码

import adal

token_response = adal.acquire_token_with_username_password(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff',
    'interface@crm.my_domain.com',
    'my_password'
)

在这两种情况下,token_response都会获得一个token-object,包含accessToken,refreshToken,expiresIn等.所以我认为到目前为止还没有错误.

然后我尝试向Web API发出一个简单的请求:

headers = {'Authorization': '%s %s' % (token_response.get('tokenType'),
                                       token_response.get('accessToken'))}

r = requests.get('https://domain.api.crm4.dynamics.com/api/data/v8.0/Product',
                 headers=headers)

这总是返回HTTP 401 – 未授权:访问被拒绝.

('WWW-Authenticate', 'Bearer error=invalid_token,
error_description=Error during token validation!,
authorization_uri=https://login.windows.net/eabcdefgh-1234-5678-a1b1-morerandomstuff/oauth2/authorize,
resource_id=https://domain.api.crm4.dynamics.com/')

尝试发出请求的用户具有Office-365-Administrator权限,并且在CRM中具有所有管理员角色和管理员角色.根据我的口味,这甚至有点多,但我在某处读到,用户必须拥有office-365管理员权限.
在Azure AD中,配置了一个应用程序,该应用程序具有对CRM的“委派权限”(“作为组织用户访问CRM Online”).

我在这里错过了什么?或者我的REST-get-request错了?
新API的Microsoft文档几乎不存在 – 每当您单击某个链接时,您将获得旧API(组织API等)的文档

解决方法:

acquire_token_with_username_password有一个optional parameter,用于指定要访问的资源:

resource (str, optional): The resource you are accessing. Defaults to
‘07001’.

因此,您应该能够通过添加资源作为acquire_token_with_username_password的参数来指定您想要访问CRM:

token_response = adal.acquire_token_with_username_password(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff',
    'interface@crm.my_domain.com',
    'my_password',
    resource='https://domain.crm4.dynamics.com'
)

这应该为您提供访问CRM的正确令牌.

获取正确的令牌后,您还需要稍微修改Web API调用(从产品到产品):

r = requests.get('https://domain.api.crm4.dynamics.com/api/data/v8.0/products',
                 headers=headers)

标签:python,rest,azure-active-directory,adal,dynamics-crm-2016
来源: https://codeday.me/bug/20190523/1156754.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有