使用 Rest API 在 MS Dynamics 中添加附件以引导
有谁知道如何使用 Rest API 将附件上传到 MS Dynamics 中的现有主导实体? 我尝试关注此帖子使用 python 代码,但收到错误消息
Python 代码
def upload_file_to_entity(url, attachment_data, version, file_name, entity_id, auth_token):
file_upload_url = f'{url}/api/data/{version}/annotations'
header_values = {
'Authorization': auth_token,
'Content-Type': 'application/json'
}
file_body = {
'subject': 'Attachment Upload',
'filename': file_name,
'[email protected]': f'/leads({entity_id})',
'documentbody': attachment_data
}
file_upload_res = requests.post(file_upload_url, headers=header_values, data=file_body)
print(file_upload_res)
print(file_upload_res.content)
attachment_object = s3.get_object(Bucket=bucket_name, Key=attachment_file_location)
attachment_data = attachment_object.get('Body').read()
attachment_data_base64 = base64.b64encode(attachment_data)
base64_string = attachment_data_base64.decode('utf-8')
upload_file_to_entity(f'https://{org}.crm.dynamics.com',base64_string,'v9.2',file_name,lead_id,prm_auth_token)
Does anyone know how to upload an attachment to existing lead entity in MS Dynamics using Rest API?
I tried to follow this post using python code, but got an error saying
<Response [400]>
b'{"error":{"code":"0x0","message":"An error occurred while validating input parameters: System.ArgumentException: Stream was not readable.\r\n at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen)\r\n at System.IO.StreamReader..ctor(Stream stream, Encoding encoding)\r\n at Microsoft.OData.JsonLight.ODataJsonLightInputContext.CreateTextReader(Stream messageStream, Encoding encoding)\r\n at Microsoft.OData.JsonLight.ODataJsonLightInputContext..ctor(ODataMessageInfo messageInfo, ODataMessageReaderSettings messageReaderSettings)\r\n at Microsoft.OData.Json.ODataJsonFormat.CreateInputContext(ODataMessageInfo messageInfo, ODataMessageReaderSettings messageReaderSettings)\r\n at Microsoft.OData.ODataMessageReader.ReadFromInput[T](Func`2 readFunc, ODataPayloadKind[] payloadKinds)\r\n at System.Web.OData.Formatter.Deserialization.ODataResourceDeserializer.Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext)\r\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)"}}'
Python Code
def upload_file_to_entity(url, attachment_data, version, file_name, entity_id, auth_token):
file_upload_url = f'{url}/api/data/{version}/annotations'
header_values = {
'Authorization': auth_token,
'Content-Type': 'application/json'
}
file_body = {
'subject': 'Attachment Upload',
'filename': file_name,
'[email protected]': f'/leads({entity_id})',
'documentbody': attachment_data
}
file_upload_res = requests.post(file_upload_url, headers=header_values, data=file_body)
print(file_upload_res)
print(file_upload_res.content)
attachment_object = s3.get_object(Bucket=bucket_name, Key=attachment_file_location)
attachment_data = attachment_object.get('Body').read()
attachment_data_base64 = base64.b64encode(attachment_data)
base64_string = attachment_data_base64.decode('utf-8')
upload_file_to_entity(f'https://{org}.crm.dynamics.com',base64_string,'v9.2',file_name,lead_id,prm_auth_token)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重要提示:注释实体内的documentbody属性是文本,因此您应该放置Base64编码值,例如,如果您想存储包含快速的文本文件棕色狐狸跳过懒狗 Base64值为VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
您可以使用我的工具Dataverse REST Builder生成请求,可作为托管解决方案或 XrmToolBox 工具使用,位于存储库内是视频链接和解释如何使用它的博客文章。
创建请求并测试它是否在 Dataverse REST Builder 中按预期工作后。
我的工具不提供 Python 语法,但您可以将集合导出到 Postman。
在 Postman 中导入后,您可以根据请求生成 Python 代码(您可以在右侧菜单上看到一个 按钮)。
请记住,您需要提供承载令牌才能使用 Web API 执行请求(可能您已经这样做了,因为您收到的错误是在身份验证验证后发生的)
Important note: documentbody attribute inside annotation entity is a Text so you should put the Base64 encoded value, for example if you want to store a text file containing The quick brown fox jumps over the lazy dog the Base64 value is VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
You can generate the request using my tool Dataverse REST Builder, is available as Managed Solution or XrmToolBox tool, inside the repository there are links to a video and a blog post explaining how to use it.
After you create the request and test if it works as expected inside Dataverse REST Builder.
My tool doesn't offer a Python syntax but you can export the collection to Postman.
After you imported inside Postman you can generate the Python code from the request (you can see a </> button on the right menu).
Keep in mind that you will need to provide the Bearer Token in order to execute the request with Web API (probably you are already doing that as the error you are receiving it happens after the authentication is validated)
好的,我成功了。
除了请求对象中的 json 参数之外,一切都是正确的。正确的请求如下
Okay, I got it working.
Everything is correct except for the json parameter in the request object. The correct request is as below