gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:48:59 27 4
gpt4 key购买 nike

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

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

客户端凭据

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 管理员权限,并且在 CRM 中具有所有经理角色和管理员角色。就我的口味而言,这甚至有点多,但我在某处读到,用户必须拥有 Office-365 管理员权限。在 Azure AD 中,配置了一个应用程序,该应用程序具有 CRM 的“委派权限”(“以组织用户身份访问 CRM Online”)。

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

最佳答案

acquire_token_with_username_passwordoptional parameter用于指定您要访问的资源:

resource (str, optional): The resource you are accessing. Defaults to 'https://management.core.windows.net/'.

因此,您应该能够通过将资源添加为 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 的正确 token 。

获得正确的 token 后,您还需要稍微修改您的 Web API 调用(从 Productproducts):

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

关于python - 使用 MS Dynamics CRM 2016 的 REST Web API 在线进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com