通过Beatbox和Python向Salesforce API上传附件
我正在使用beatbox和python将文件上传到Salesforce,文件附加得很好,但文件里的数据完全损坏了。
def Send_File():
import beatbox
svc = beatbox.Client() # instantiate the object
svc.login(login1, pw1) # login using your sf credentials
update_dict = {
'type':'Attachment',
'ParentId': accountid,
'Name': 'untitled.txt',
'body':'/Users/My_Files/untitled.txt',
}
results2 = svc.create(update_dict)
print results2
输出结果是:
00Pi0000005ek6gEAAtrue
所以数据传输得不错,但当我去查看Salesforce记录00Pi0000005ek6gEAA里的文件时,文件的内容是:
˝KÆœ Wøä ï‡Îä˜øHÅCj÷øaÎ0j∑ø∫{b∂Wù
我完全不知道是什么原因导致这个问题,而且找不到其他人遇到过类似情况。
链接到 SFDC关于上传的文档
1 个回答
3
字典里的 'body' 值应该是文件内容经过 base64 编码后的结果,而不是文件名。你需要自己去读取并编码文件的内容。比如:
body = ""
with open("/Users/My_Files/untitled.txt", "rb") as f:
body = f.read().encode("base64")
update_dict = {
'type' : 'Attachement'
'ParentId' : accountId,
'Name' : 'untitled.txt',
'Body' : body }
...
关于 附件 的文档