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

时间:2016-01-04 13:57:02

标签: 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都会获得一个令牌对象,其中包含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等)的文档。

1 个答案:

答案 0 :(得分:4)

SELECT a.* FROM article a LEFT JOIN OrderItems o ON a.anr = o.artnr WHERE o.artnr IS NULL AND a.price = (SELECT TOP 1 a.price FROM article a LEFT JOIN OrderItems o ON a.anr = o.artnr WHERE o.artnr IS NULL Order By a.price Desc ) 有一个optional parameter,用于指定您要访问的资源:

  

resource(str,optional):您正在访问的资源。默认为   'https://management.core.windows.net/'。

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

acquire_token_with_username_password

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

获取正确的令牌后,您还需要稍微修改您的Web API调用(从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' ) Product):

products