我在上传此图片时收到此错误 "media type unrecognized"?
I am getting this error "media type unrecognized" when I upload this image?
上传这些类型的图片后出现此错误 ("Media type unrecognized.")。将此图像直接上传到 Twitter 可以正常工作,但不能通过 API。这是图片:
图片:
我的代码:
images = []
for x in user_tweet_images_list: # AWS S3 image urls
media_file = tempfile.NamedTemporaryFile(
delete=False,
suffix=get_image_extension(x.tweet_image.url)
)
media_file.write(x.tweet_image.read())
images.append(media_file.name)
media_ids = [api.media_upload(i).media_id_string for i in images]
post_tweet = api.update_status(status=user_tweet.tweet_message, media_ids=media_ids)
编辑:这个错误实际上是 TweepError
来自 Twitter 的 API。可能的问题是您正在上传一个空文件,因为您没有将数据刷新到临时文件,例如media_file.flush()
.
这可能是由于 a known Tweepy issue and will probably be fixed when v3.9 is released with a fix。
同时, 根据 get_image_extension
的作用,您可能需要添加一个点 (.
):
[NamedTemporaryFile] does not put a dot between the file name and the suffix; if you need one, put it at the beginning of suffix.
上传这些类型的图片后出现此错误 ("Media type unrecognized.")。将此图像直接上传到 Twitter 可以正常工作,但不能通过 API。这是图片:
图片:
我的代码:
images = []
for x in user_tweet_images_list: # AWS S3 image urls
media_file = tempfile.NamedTemporaryFile(
delete=False,
suffix=get_image_extension(x.tweet_image.url)
)
media_file.write(x.tweet_image.read())
images.append(media_file.name)
media_ids = [api.media_upload(i).media_id_string for i in images]
post_tweet = api.update_status(status=user_tweet.tweet_message, media_ids=media_ids)
编辑:这个错误实际上是 TweepError
来自 Twitter 的 API。可能的问题是您正在上传一个空文件,因为您没有将数据刷新到临时文件,例如media_file.flush()
.
这可能是由于 a known Tweepy issue and will probably be fixed when v3.9 is released with a fix。
同时, 根据 get_image_extension
的作用,您可能需要添加一个点 (.
):
[NamedTemporaryFile] does not put a dot between the file name and the suffix; if you need one, put it at the beginning of suffix.