我正在尝试访问新的REST,用于构建服务器到服务器的接口,以便将CRM与其他应用程序(如web商店等)集成起来。
我尝试过从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提出一个简单的请求:
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在线”)。
我在这里错过了什么?或者我的休息请求是错的?新API的Microsoft文档实际上是不存在的--只要单击某些链接,就会获得旧API (组织API等)的文档。
发布于 2016-01-05 11:38:30
acquire_token_with_username_password有一个可选参数,用于指定您想要访问的资源:
资源(str,可选):您正在访问的资源。默认为“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提供一个适当的令牌。
在获得正确的令牌之后,还需要稍微修改Web调用(从Product到products):
r = requests.get('https://domain.api.crm4.dynamics.com/api/data/v8.0/products',
headers=headers)https://stackoverflow.com/questions/34592784
复制相似问题