環境
Windows10
Python3.8
TwitterAPIの勉強をしております。
初学者です。
下記のコードを書きました。
Twitterのタイムラインの情報を取得するのですが、エラーが発生します。
consumer_key = "" , consumer_secret = "" access_token = "" access_token_secret = "" !pip install tweepy import tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) public_tweets = api.home_timeline() public_tweets
Twitterのタイムラインの情報を取得するのですが、エラーが発生します。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py in execute(self) 183 try: --> 184 resp = self.session.request(self.method, 185 full_url, C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 527 ) --> 528 prep = self.prepare_request(req) 529 C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in prepare_request(self, request) 455 p = PreparedRequest() --> 456 p.prepare( 457 method=request.method.upper(), C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json) 319 self.prepare_body(data, files, json) --> 320 self.prepare_auth(auth, url) 321 C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare_auth(self, auth, url) 550 # Allow auth to make its changes. --> 551 r = auth(self) 552 C:\ProgramData\Anaconda3\lib\site-packages\requests_oauthlib\oauth1_auth.py in __call__(self, r) 107 # Omit body data in the signing of non form-encoded requests --> 108 r.url, headers, _ = self.client.sign( 109 unicode(r.url), unicode(r.method), None, r.headers C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in sign(self, uri, http_method, body, headers, realm) 350 request.oauth_params.append( --> 351 ('oauth_signature', self.get_oauth_signature(request))) 352 C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in get_oauth_signature(self, request) 164 --> 165 uri, headers, body = self._render(request) 166 C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in _render(self, request, formencode, realm) 246 if self.signature_type == SIGNATURE_TYPE_AUTH_HEADER: --> 247 headers = parameters.prepare_headers( 248 request.oauth_params, request.headers, realm=realm) C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py in wrapper(params, *args, **kwargs) 24 params = filter_oauth_params(params) ---> 25 return target(params, *args, **kwargs) 26 C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py in prepare_headers(oauth_params, headers, realm) 52 escaped_name = utils.escape(oauth_parameter_name) ---> 53 escaped_value = utils.escape(value) 54 C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py in escape(u) 48 if not isinstance(u, str): ---> 49 raise ValueError('Only unicode objects are escapable. ' + 50 'Got {!r} of type {}.'.format(u, type(u))) ValueError: Only unicode objects are escapable. Got <generator object to_unicode.<locals>.<genexpr> at 0x000001B9CE23E3C0> of type <class 'generator'>. During handling of the above exception, another exception occurred: TweepError Traceback (most recent call last) <ipython-input-8-ad72c34536d3> in <module> ----> 1 public_tweets = api.home_timeline() 2 public_tweets C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py in _call(*args, **kwargs) 251 return method 252 else: --> 253 return method.execute() 254 finally: 255 method.session.close() C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py in execute(self) 190 proxies=self.api.proxy) 191 except Exception as e: --> 192 six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2]) 193 194 rem_calls = resp.headers.get('x-rate-limit-remaining') C:\ProgramData\Anaconda3\lib\site-packages\six.py in reraise(tp, value, tb) 700 value = tp() 701 if value.__traceback__ is not tb: --> 702 raise value.with_traceback(tb) 703 raise value 704 finally: C:\ProgramData\Anaconda3\lib\site-packages\tweepy\binder.py in execute(self) 182 # Execute request 183 try: --> 184 resp = self.session.request(self.method, 185 full_url, 186 data=self.post_data, C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 526 hooks=hooks, 527 ) --> 528 prep = self.prepare_request(req) 529 530 proxies = proxies or {} C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in prepare_request(self, request) 454 455 p = PreparedRequest() --> 456 p.prepare( 457 method=request.method.upper(), 458 url=request.url, C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json) 318 self.prepare_cookies(cookies) 319 self.prepare_body(data, files, json) --> 320 self.prepare_auth(auth, url) 321 322 # Note that prepare_auth must be last to enable authentication schemes C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in prepare_auth(self, auth, url) 549 550 # Allow auth to make its changes. --> 551 r = auth(self) 552 553 # Update self to reflect the auth changes. C:\ProgramData\Anaconda3\lib\site-packages\requests_oauthlib\oauth1_auth.py in __call__(self, r) 106 else: 107 # Omit body data in the signing of non form-encoded requests --> 108 r.url, headers, _ = self.client.sign( 109 unicode(r.url), unicode(r.method), None, r.headers 110 ) C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in sign(self, uri, http_method, body, headers, realm) 349 # generate the signature 350 request.oauth_params.append( --> 351 ('oauth_signature', self.get_oauth_signature(request))) 352 353 # render the signed request and return it C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in get_oauth_signature(self, request) 163 self.resource_owner_secret) 164 --> 165 uri, headers, body = self._render(request) 166 167 collected_params = signature.collect_parameters( C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py in _render(self, request, formencode, realm) 245 # I'm not sure how I feel about it. 246 if self.signature_type == SIGNATURE_TYPE_AUTH_HEADER: --> 247 headers = parameters.prepare_headers( 248 request.oauth_params, request.headers, realm=realm) 249 elif self.signature_type == SIGNATURE_TYPE_BODY and request.decoded_body is not None: C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py in wrapper(params, *args, **kwargs) 23 def wrapper(params, *args, **kwargs): 24 params = filter_oauth_params(params) ---> 25 return target(params, *args, **kwargs) 26 27 wrapper.__doc__ = target.__doc__ C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py in prepare_headers(oauth_params, headers, realm) 51 # .. _`Section 3.6`: https://tools.ietf.org/html/rfc5849#section-3.6 52 escaped_name = utils.escape(oauth_parameter_name) ---> 53 escaped_value = utils.escape(value) 54 55 # 2. Each parameter's name is immediately followed by an "=" character C:\ProgramData\Anaconda3\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py in escape(u) 47 """ 48 if not isinstance(u, str): ---> 49 raise ValueError('Only unicode objects are escapable. ' + 50 'Got {!r} of type {}.'.format(u, type(u))) 51 # Letters, digits, and the characters '_.-' are already treated as safe TweepError: Failed to send request: Only unicode objects are escapable. Got <generator object to_unicode.<locals>.<genexpr> at 0x000001B9CE23E3C0> of type <class 'generator'>.
検索しても似たようなエラーがなくて困っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/07/09 09:49