如何在python中对twitter进行base64编码?

时间:2014-11-25 06:21:56

标签: python twitter base64 twython

所以我一直在尝试(没有运气)使用Twython库对图像进行base64编码上传到twitter。我已经按照我知道的方式完成了它但它似乎无法工作。

from twython import Twython
import base64

#keys
APP_KEY = '*************'
APP_SECRET = '**************'
OAUTH_TOKEN = '**********************'
OAUTH_TOKEN_SECRET = '*************************'
#start twitter instance
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

#encode image to bas64
with open("image.jpeg", "rb") as imageF:
    st = base64.b64encode(imageF.read())
    #print to make sure its being encoded
    print st

#update twitter banner
twitter.update_profile_background_image(image=st)

Twitter文档并没有多大帮助。 https://dev.twitter.com/rest/reference/post/account/update_profile_banner Twython Docs也不是。 https://twython.readthedocs.org/en/latest/api.html 让我知道你会怎么做。或者如果你有更好的方法。

由于

2 个答案:

答案 0 :(得分:0)

我认为它是'banner'而不是'image'

twitter.update_profile_background_image(banner=st)

答案 1 :(得分:0)

好的,所以我想我会回来说出我发现的东西。我这样做的方式有效。我之前显然已使用无效参数进行相同的调用,并且twitter将所有API调用作为调用计数。他们似乎不会让你经常进行横幅更新通话。另外我认为他们希望图像尺寸合适,似乎是(1500px,500px)所以你应该在编码之前尝试调整图像大小。如果有人知道更简单的方式......我仍然愿意接受建议。

谢谢你!