3

我想使用 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.Update_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.

有什么想法吗?谢谢!

4

4 回答 4

2

要通过表单正确提交 ah 图像,您必须包括

enctype="multipart/form-data" 

例如

<form  enctype="multipart/form-data" action='/' method="POST">
于 2012-07-15T09:38:47.753 回答
2

Twitter RESTful API Document是不正确的。
不要编码 image binarybase64! 从源中删除base64编码部分。

如果你编码image binarybase64字符串,twitterapi 说

“...是您的图片有问题,可能太大了。(...)(代码 131)”

于 2012-11-16T07:35:34.803 回答
1

根据文档,您的图像:

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

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

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

于 2012-07-12T21:56:38.717 回答
1

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

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

于 2012-07-13T17:01:52.550 回答