简体   繁体   English

使用python和simple_salesforce将文件上传到salesforce为空

[英]File uploaded to salesforce empty using python and simple_salesforce

I'm trying to upload a file to the Folder object in salesforce using python and simple_salesforce. 我正在尝试使用python和simple_salesforce将文件上传到salesforce中的Folder对象。 The file is uploaded but is empty. 该文件已上传,但为空。 can anyone tell me why and how to fix the problem? 谁能告诉我原因以及如何解决该问题? Thanks. 谢谢。

import base64
import json
from simple_salesforce import Salesforce

userName = 'username'
passWord = 'password'
securitytoken = 'securitytoken'

sf=Salesforce(username='userName', password='passWord', security_token='securitytoken', sandbox = True)
sessionId = sf.session_id

body = ""
with open("Info.txt", "r") as f:
    body = base64.b64encode(f.read())

response = requests.post('https://cs17.my.salesforce.com/services/data/v23.0/sobjects/Document/',
headers = { 'Content-type': 'application/json', 'Authorization':'Bearer %s' % sessionId},
data = json.dumps({
    'Description':'Information',
    'Keywords':'Information',
    'FolderId': '00lg0000000MQykAAG',
    'Name': 'Info',
    'Type':'txt'
    })
)

print response.text

Based on the code above, your request is incomplete. 根据以上代码,您的请求不完整。 You create the body value as the b64encoding iof the file you wish to upload, but it isn't included in your data associated with a 'body' key. 您可以将主体值创建为要上传的文件的b64encoding,但是它不包含在与“主体”键关联的数据中。

Depending on your version of json, you may experience some problems getting the output to play nice with Salesforce. 根据您的json版本,您可能会遇到一些问题,无法使输出与Salesforce配合使用。 I switched from json to simplejson and that worked better for me. 我从json切换到simplejson,对我来说效果更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM