Python请求Tumblr照片上传

2024-04-18 17:45:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Python的requests模块将照片上传到tumblr。我已成功登录,并进行了一些POST方法,只有一个错误(我已接近上传过程的结尾):

session.headers = {'referer': 'https://www.tumblr.com/blog/myblogname/new/photo',
                'User-Agent': ua,
                'x-requested-with': 'XMLHttpRequest',
                'x-tumblr-form-key': form_key}

files ={image_to_post : open(image_to_post,mode="r")}

post_photo = session.post('https://www.tumblr.com/svc/post/upload_photo',    headers =session.headers, cookies=cookie, files = files )

tumblr post方法所需的有效负载:

------WebKitFormBoundaryORrQGzdK9p9jH0Aq
Content-Disposition: form-data; name="photo"; filename="nameofimage.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryORrQGzdK9p9jH0Aq--

那个WebKitForm的好格式是什么?你知道吗

问题在于文件的打开/选择,因为如果我添加了不好的数据,比如一个url或什么,它会说:这很好,但它不是一张格式正确的照片,请将它保存到jpg/png等

当前响应:

{"errors":["Please select a photo file to upload."],"post":false}

Tags: to方法keyhttpsimageformcomsession