使用twitter api和python上传个人资料图片



我想使用html表单从硬盘上传一个图像:

Image file: <input name="imageupload" id="imageupload" type="file" />

然后我把它上传到推特上:

image=self.request.get('imageupload')
image2=base64.b64encode(image)
twitapi.Update_profile_image(image=image2)

给定twitapi。更新_profile_image:

def Update_profile_image(self,image):
    if not self._oauth_consumer:
        raise TwitterError("The twitter.Api instance must be authenticated.")
    url = '%s/account/update_profile_image.json' % (self.base_url)
    data = {'image':image}
    json = self._FetchUrl(url, post_data=data)
    data = self._ParseAndCheckTwitter(json)
    return data

来自twitter api 的给定_FetchUrl

我总是收到

TwitterError: There was a problem with your picture. Probably too big.

有什么想法吗?谢谢

要通过表单正确提交图像,必须包含

enctype="multipart/form-data" 

例如

<form  enctype="multipart/form-data" action='/' method="POST">

Twitter RESTful API Document不正确
不要image binary编码为base64!从源代码中删除base64编码部分。

如果将image binary编码为base64字符串,则twitter api会显示

"…你的照片可能太大了。(…)(代码131)"

根据文档,您的图像:

Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size.

因此,请确保您的图像符合这些限制条件。也许你需要缩小你的图像,或者将其转换为不同的格式。

如果这不起作用,请尝试上传另一个符合上述限制的非常小的图像。至少你可以验证问题是否出在你使用的特定图像上。

也许您通过表单上传收到的图像已经是base64编码的?

然后,您将应用双重编码,这可能会混淆twitter服务器端的验证,因为它无法在上传的文件中找到典型的图像标头。

最新更新