我有一些非常简单的代码,可以通过Tweepy库将图像上传到Twitter,然后使用返回的媒体ID发布状态更新。我在这里看到了很多关于这个主题的问题,但没有一个能解决我的问题。代码如下。
import tweepy
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('/var/www/config.ini')
CONSUMER_KEY = config.get('twitter', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('twitter', 'CONSUMER_SECRET')
ACCESS_KEY = config.get('twitter', 'ACCESS_KEY')
ACCESS_SECRET = config.get('twitter', 'ACCESS_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
file = open('/var/www/photo1.jpeg', 'rb')
r1 = api.media_upload(filename='/var/www/photo1.jpeg', file=file)
print(r1)
print(r1.media_id_string)
file = open('/var/www/photo2.jpeg', 'rb')
r2 = api.media_upload(filename='/var/www/photo2.jpeg', file=file)
print(r2)
print(r2.media_id_string)
media_ids = r1.media_id_string + ', ' + r2.media_id_string
print(media_ids)
api.update_status(media_ids=media_ids, status="Test Tweet")
执行此脚本时,我在最后一行收到以下错误
Traceback (most recent call last):
File "test2.py", line 26, in <module>
api.update_status(media_ids=media_ids, status="Test Tweet")
File "/usr/local/lib/python3.4/dist-packages/tweepy/api.py", line 194, in update_status
)(post_data=post_data, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.4/dist-packages/tweepy/binder.py", line 229, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'message': 'media_ids parameter is invalid.', 'code': 44}]
2个媒体上传请求返回以下对象:
Media(media_id=728190961679929344, size=879715, expires_after_secs=86400,
media_id_string='728190961679929344', _api=<tweepy.api.API object at
0x7ffaf4d8fda0>, image={'h': 4000, 'w': 5000, 'image_type': 'image/jpeg'})
和
Media(media_id=728190987122532353, size=17489, expires_after_secs=86400,
media_id_string='728190987122532353', _api=<tweepy.api.API object at
0x7ffaf4d8fda0>, image={'h': 369, 'w': 640, 'image_type': 'image/jpeg'})
我通过media_id_string变量从中提取728190961679929344
和728190987122532353
的媒体ID作为字符串,并将它们组合成一个用逗号分隔的单个字符串,即728190961679929344, 728190987122532353
。我尝试过使用和不使用空格,单引号和双引号,单引引用并引用整个字符串但没有任何作用。
如果相反,我尝试使用单个图片ID进行更新,如下所示
import tweepy
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('/var/www/config.ini')
CONSUMER_KEY = config.get('twitter', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('twitter', 'CONSUMER_SECRET')
ACCESS_KEY = config.get('twitter', 'ACCESS_KEY')
ACCESS_SECRET = config.get('twitter', 'ACCESS_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
file = open('/var/www/photo1.jpeg', 'rb')
r1 = api.media_upload(filename='/var/www/photo1.jpeg', file=file)
print(r1)
print(r1.media_id_string)
file = open('/var/www/photo2.jpeg', 'rb')
r2 = api.media_upload(filename='/var/www/photo2.jpeg', file=file)
print(r2)
print(r2.media_id_string)
media_ids = r1.media_id_string + ', ' + r2.media_id_string
print(media_ids)
api.update_status(media_ids=r1.media_id_string, status="Test Tweet")
我在最后一行再次收到以下错误
Traceback (most recent call last):
File "test2.py", line 26, in <module>
api.update_status(media_ids=r1.media_id_string, status="Test Tweet")
File "/usr/local/lib/python3.4/dist-packages/tweepy/api.py", line 194, in update_status
)(post_data=post_data, *args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/tweepy/binder.py", line 245, in _call
return method.execute()
File "/usr/local/lib/python3.4/dist-packages/tweepy/binder.py", line 229, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'message': 'Tweet must not have more than 4 mediaIds.', 'code': 324}]
显然我只有1个媒体ID,所以错误没有意义。我假设我正确地格式化了请求,但我尝试了一系列不同的格式,但似乎都没有。
任何想法,因为我出去了?
提前致谢。
答案 0 :(得分:0)
原来,media_ids没有格式化为字符串,而是字符串列表,这与Twitter API文档不同,因此Tweepy必须在打包前格式化列表中的请求。这是我的代码首先是多个图像:
import tweepy
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('/var/www/config.ini')
CONSUMER_KEY = config.get('twitter', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('twitter', 'CONSUMER_SECRET')
ACCESS_KEY = config.get('twitter', 'ACCESS_KEY')
ACCESS_SECRET = config.get('twitter', 'ACCESS_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
file = open('/var/www/photo1.jpeg', 'rb')
r1 = api.media_upload(filename='/var/www/photo1.jpeg', file=file)
print(r1)
print(r1.media_id_string)
file = open('/var/www/photo2.jpeg', 'rb')
r2 = api.media_upload(filename='/var/www/photo2.jpeg', file=file)
print(r2)
print(r2.media_id_string)
media_ids = [r1.media_id_string, r2.media_id_string]
print(media_ids)
api.update_status(media_ids=media_ids, status="Test Tweet")
然后是单张图片:
import tweepy
from configparser import SafeConfigParser
config = SafeConfigParser()
config.read('/var/www/config.ini')
CONSUMER_KEY = config.get('twitter', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('twitter', 'CONSUMER_SECRET')
ACCESS_KEY = config.get('twitter', 'ACCESS_KEY')
ACCESS_SECRET = config.get('twitter', 'ACCESS_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
file = open('/var/www/photo1.jpeg', 'rb')
r1 = api.media_upload(filename='/var/www/photo1.jpeg', file=file)
print(r1)
print(r1.media_id_string)
file = open('/var/www/photo2.jpeg', 'rb')
r2 = api.media_upload(filename='/var/www/photo2.jpeg', file=file)
print(r2)
print(r2.media_id_string)
media_ids = [r1.media_id_string_ids]
print(media_ids)
api.update_status(media_ids=media_ids, status="Test Tweet")