在 twython 中使用图像更新 Twitter 状态,不断出现 API 错误?
Update Twitter status with an image in twython, keep getting API errors?
编辑:更新了代码。不断收到 API 错误。
如何使用 twython 和 python 3x post 来自 URL 且状态更新到 Twitter 的图像?我已经阅读了文档,但它只概述了在目录中打开本地文件,并且我已经查看了一些关于 SO 的线程。到目前为止,这是我遇到的错误代码,我不知道如何解决。
我要更改什么才能将此图像变为 post?这给了我 twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you.
它到达的那一刻 twitter.upload
from twython import Twython, TwythonError
import os
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = requests.get(response.url).content
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
这个解决方案:twitter.post('/statuses/update_with_media', params = {'status': 'Testing New Status'}, files = {'media': StringIO(photo)})
给我一个 TypeError: initial_value must be str or None, not bytes
我无法让它工作。我不知道为什么。它是一个主帐户。我没有使用 "sign in with twitter",它不是网络应用程序。只是一个工人机器人。
我很困惑,我想不通我做错了什么。它将 post 文本状态正常,我可以搜索、获取用户时间线、发送 DMS 等。我做错了什么?
事实证明这是一个类型错误,与 twython 在上传媒体时所期望的有关。远程拉下图像并在其上调用内容给我们一个 bytes
但 twython 想要一个 io
对象。
堆栈跟踪中的 401 是一个转移注意力的问题,但我们可以通过添加 io
并将请求内容读入 BytesIO
.[=15= 来为 twython 提供它想要的东西]
from twython import Twython, TwythonError
from io import BytesIO
import os
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = BytesIO(response.content)
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
编辑:更新了代码。不断收到 API 错误。
如何使用 twython 和 python 3x post 来自 URL 且状态更新到 Twitter 的图像?我已经阅读了文档,但它只概述了在目录中打开本地文件,并且我已经查看了一些关于 SO 的线程。到目前为止,这是我遇到的错误代码,我不知道如何解决。
我要更改什么才能将此图像变为 post?这给了我 twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you.
它到达的那一刻 twitter.upload
from twython import Twython, TwythonError
import os
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = requests.get(response.url).content
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])
这个解决方案:twitter.post('/statuses/update_with_media', params = {'status': 'Testing New Status'}, files = {'media': StringIO(photo)})
给我一个 TypeError: initial_value must be str or None, not bytes
我无法让它工作。我不知道为什么。它是一个主帐户。我没有使用 "sign in with twitter",它不是网络应用程序。只是一个工人机器人。
我很困惑,我想不通我做错了什么。它将 post 文本状态正常,我可以搜索、获取用户时间线、发送 DMS 等。我做错了什么?
事实证明这是一个类型错误,与 twython 在上传媒体时所期望的有关。远程拉下图像并在其上调用内容给我们一个 bytes
但 twython 想要一个 io
对象。
堆栈跟踪中的 401 是一个转移注意力的问题,但我们可以通过添加 io
并将请求内容读入 BytesIO
.[=15= 来为 twython 提供它想要的东西]
from twython import Twython, TwythonError
from io import BytesIO
import os
import urllib
import requests
CONSUMER_KEY = os.environ['CONSUMER_KEY']
CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
twitter.verify_credentials()
url = "https://farm2.staticflickr.com/1127/4605090363_4a96e64ff1.jpg"
response = requests.get(url)
photo = BytesIO(response.content)
response = twitter.upload_media(media=photo)
twitter.update_status(status='Checkout this cool image!', media_ids=[response['media_id']])