我很难找到如何通过Python TwitterAPI向Tweet添加图片的文档。有什么想法吗?
这是我到目前为止所拥有的:
consumer_key = ' '
consumer_secret = ' '
access_token_key = ' '
access_token_secret = ' '
from TwitterAPI import TwitterAPI
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
file = open('image.jpg', 'rb')
data = file.read()
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
print(r.status_code)
输出:
Traceback (most recent call last):
File "tweet_media.py", line 48, in <module>
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
TypeError: request() takes at most 3 arguments (4 given)
答案 0 :(得分:8)
此示例将上传包含嵌入图像的推文。您需要使用TwitterAPI 2.1.8.7或更高版本。
from TwitterAPI import TwitterAPI
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN_KEY = ''
ACCESS_TOKEN_SECRET = ''
api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
file = open('Your_image.png', 'rb')
data = file.read()
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
print(r.status_code)
答案 1 :(得分:0)
两个选项:
update_with_media
API端点将文件上传到Twitter本身。