【问题标题】:Making a request to Dynamics CRM Web API向 Dynamics CRM Web API 发出请求
【发布时间】:2017-06-06 19:17:04
【问题描述】:

我正在尝试制作一个使用 urllib2 从 python 向 Dynamics CRM Web API 发出请求的应用程序。 到目前为止,我可以通过向https://login.microsoftonline.com/common/oauth2/authorize 发出发布请求来使用 Azure 应用程序登录用户 然后使用检索到的授权码,我可以使用 urllib2 获取 access_token、refresh_token 和其他人

url = 'https://login.microsoftonline.com/common/oauth2/token'
post_fields = {'grant_type': 'authorization_code',
                'client_id': CLIENT_ID,
                'client_secret': CLIENT_SECRET,
                'redirect_uri': REDIRECT_URI,
                'resource': 'https://graph.microsoft.com',
                'code': code}  
request = Request(url, urlencode(post_fields).encode())
resp = urlopen(request).read().decode()
resp = json.loads(resp)
refresh_token = resp['refresh_token']
id_token = resp['id_token']
id_token = jwt.decode(id_token,verify=False)
access_token = resp['access_token']

然后我尝试使用 access_token 发出另一个帖子请求,但没有运气。 我不断得到:

HTTP 错误 401:未经授权

作为测试,我直接在 .dynamics.com/api/data/v8.1/leads 上发帖 如下:

url = 'https://<company_uri>.dynamics.com/api/data/v8.1/leads'
post_fields = {"name": "Sample Account",
                "creditonhold": "false",
                "address1_latitude": 47.639583,
                "description": "This is the description of the sample account",
                "revenue": 5000000,
                "accountcategorycode": 1
               }
request = Request(url, urlencode(post_fields).encode())
request.add_header('Authorization', 'Bearer ' + access_token )
request.add_header("Content-Type", "application/json; charset=utf-8")
request.add_header('OData-MaxVersion','4.0')
request.add_header('OData-Version','4.0')
request.add_header('Accept','application/json')
resp = urlopen(request).read().decode()

但我不断收到相同的 401 错误代码。 我查看了所有 msdn 文档,但没有找到不使用任何库直接执行此操作的方法,我只想使用一个简单的发布请求。

由于错误代码显示未经授权,我认为 access_token 必须以其他方式发送。 有人可以帮助我如何在 Dynamics CRM 上正确使用 access_token 吗? 谢谢!

【问题讨论】:

    标签: python azure urllib2 crm microsoft-dynamics


    【解决方案1】:

    您取回的访问令牌用于 Azure AD Graph API。不是 Dynamics CRM。

    要调用它,您必须请求一个访问令牌,其中 resource 设置为 Dynamics CRM API 的 App ID URI,而不是 https://graph.windows.net。

    根据documentation,您应该将resource设置为https://&lt;company_uri&gt;.crm.dynamics.com。

    所以当你检索令牌时:

    url = 'https://login.microsoftonline.com/common/oauth2/token'
    post_fields = {'grant_type': 'authorization_code',
                    'client_id': CLIENT_ID,
                    'client_secret': CLIENT_SECRET,
                    'redirect_uri': REDIRECT_URI,
                    'resource': 'https://<company_uri>.crm.dynamics.com',
                    'code': code}  
    

    【讨论】:

    • 我之前曾更改过一次资源,但从未得到答案,我总是收到“HTTP/1.1 400 Bad Request”。使用graph.microsoft.com 资源,我得到了一个代码,但现在我明白它当然很有用,因为这不是我想要授予访问权限的资源。我现在确实更改了资源,但出现 400 错误。
    • 您应该阅读回复,它会说明问题所在。
    • “用户或管理员未同意使用该应用程序”。最初的问题是因为资源错误,所以我认为这已经解决了。谢谢@juunas。
    • 是的,这是一个单独的常见问题,您肯定可以在这里找到答案。
    猜你喜欢
    • 2017-09-08
    • 2016-07-26
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多