我正在尝试使用 tweepy 通过机器人创建一条推文,但我不断收到错误(如下所示)。
我正在使用 Python 3 和 Twitter APIv2。
仅运行
api.verify_credentials
时,身份验证成功。
这些是我收到的错误(Windows 终端):
PS C:\bot> python app.py
The user credentials are valid.
Traceback (most recent call last):
File "C:\bot\app.py", line 24, in <module>
tweepy.Client.create_tweet(Self, text="hello", user_auth=True)
File "C:\Users\totes\AppData\Local\Programs\Python\Python311\Lib\site-packages\tweepy\client.py", line 835, in create_tweet
return self._make_request(
^^^^^^^^^^^^^^^^^^
File "C:\Users\totes\AppData\Local\Programs\Python\Python311\Lib\typing.py", line 450, in __getattr__
raise AttributeError(item)
AttributeError: _make_request
这是我的代码:
from typing import Self
import tweepy
consumer_key = "XXXX"
consumer_secret = "XXXX"
access_token = "XXXX"
access_token_secret = "XXXX"
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
if api.verify_credentials() == False:
print("The user credentials are invalid.")
else:
print("The user credentials are valid.")
try:
tweepy.Client.create_tweet(Self, text="hello", user_auth=True)
except tweepy.errors.TweepyException as err:
print("err")
create_tweet
是 Client
(或更正确的是 BaseClient
)类的实例方法。像静态方法一样调用它并将 typing.Self
作为实例传递给它是没有任何作用的(您可以将 api
作为实例传递并且它会起作用,但是当您使用时,Python 可以隐式传递 self
参数类似于 api.create_tweet(...)
)。
对所有内容进行进一步阅读: