質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

1453閲覧

TweepError: Failed to send request:

saito5940

総合スコア63

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/07/08 18:44

環境
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'>.

検索しても似たようなエラーがなくて困っています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

consumer_key = "" ,

consumer_key = ""

と書きたかったのにうっかり,が入ってしまったのだろうな、と思いました。
(これが原因でエラーが再現するかは分かりませんが)


,はタプルを生成する機能があります。

python

1>>> a = 1, 2 3>>> a 4(1,) 5 6 7>>> type(a) 8<class 'tuple'>

投稿2021/07/09 04:59

編集2021/07/09 05:13
quickquip

総合スコア11038

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saito5940

2021/07/09 09:49

ありがとうございます。 何で、そのカンマを見落として苦しまなければならない・・。 助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問