Я новичок в python и пытаюсь создать бота, который подключается к моей учетной записи в твиттере. У меня есть некоторый базовый код для доступа к моей учетной записи с моими учетными данными разработчика в Твиттере, но когда я пытаюсь проверить его, используя:
API = create_api()
API.update_status('Hello World!')'
, я получаю ValueError: только объекты Unicode могут быть сброшены. Не получил тип из библиотеки oauth. Который вызывает ошибку tweepy того же типа: tweepy.error.TweepError: Не удалось отправить запрос: только объекты Unicode могут быть экранированы. У меня нет типа.
Как мне устранить эту ошибку?
Я пытался из future импортировать unicode_literals, но я все еще получаю ту же ошибку.
CONSUMER_KEY = consumer_keys
CONSUMER_SECRET = consumer_secrets
ACCESS_TOKEN = access_tokens
ACCESS_TOKEN_SECRET = access_token_secrets
logger = logging.getLogger()
def create_api():
consumer_key = os.getenv(CONSUMER_KEY)
consumer_secret = os.getenv(CONSUMER_SECRET)
access_token = os.getenv(ACCESS_TOKEN)
access_token_secret = os.getenv(ACCESS_TOKEN_SECRET)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
# api object is being used to talk to twitter
try:
api.verify_credentials()
except Exception as e:
logger.error("Error creating API", exc_info=True)
raise e
logger.info("API created")
return api
API = create_api()
API.update_status('Hello World!')
Список ошибок:
C:\Anaconda3\envs\TwitterBot\pythonw.exe
C:\PycharmProjects/TwitterBot/config.py
Error creating API
Traceback (most recent call last):
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 189, in execute
proxies=self.api.proxy)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_auth(auth, url)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 548, in prepare_auth
r = auth(self)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 210, in _render
request.oauth_params, request.headers, realm=realm)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
'Got %r of type %s.' % (u, type(u)))
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/PycharmProjects/TwitterBot/config.py", line 28, in create_api
api.verify_credentials()
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\api.py", line 605, in verify_credentials
)(**kargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 250, in _call
return method.execute()
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 191, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 189, in execute
proxies=self.api.proxy)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_auth(auth, url)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 548, in prepare_auth
r = auth(self)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 210, in _render
request.oauth_params, request.headers, realm=realm)
File "C:\Anaconda3\envs\TwitterBot\lib\site- packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
'Got %r of type %s.' % (u, type(u)))
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
Traceback (most recent call last):
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 189, in execute
proxies=self.api.proxy)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_auth(auth, url)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 548, in prepare_auth
r = auth(self)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 210, in _render
request.oauth_params, request.headers, realm=realm)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
'Got %r of type %s.' % (u, type(u)))
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/PycharmProjects/TwitterBot/config.py", line 36, in <module>
API = create_api()
File "C:/PycharmProjects/TwitterBot/config.py", line 31, in create_api
raise e
File "C:/PycharmProjects/TwitterBot/config.py", line 28, in create_api
api.verify_credentials()
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\api.py", line 605, in verify_credentials
)(**kargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 250, in _call
return method.execute()
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 191, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\tweepy\binder.py", line 189, in execute
proxies=self.api.proxy)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_auth(auth, url)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests\models.py", line 548, in prepare_auth
r = auth(self)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 210, in _render
request.oauth_params, request.headers, realm=realm)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Anaconda3\envs\TwitterBot\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
'Got %r of type %s.' % (u, type(u)))
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
Process finished with exit code 1