How to solve AttributeError: 'Client' object has no attribute 'apply_auth'?

How to solve AttributeError: 'Client' object has no attribute 'apply_auth'?

我在处理 tweepy 的新旧文档时遇到了问题,似乎在以前的版本中一切都有效,但有很多变化,我现在无法让它正常工作。知道为什么会出现此错误吗?

import tweepy

client = tweepy.Client(bearer_token='[redacted]', 
                       consumer_key='[redacted]', 
                       consumer_secret='[redacted]', 
                       access_token='[redacted]', 
                       access_token_secret='[redacted]')


api = tweepy.API(client)
public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)

我收到这个错误

AttributeError: 'Client' object has no attribute 'apply_auth'

首先,您应该立即刷新所有个人和应用程序令牌。

任何人都可以代表您访问 Twitter API,这就像公开分享您的密码。

关于你的问题:tweepy.Client用于访问2版推特API,tweepy.API用于访问1.1版推特api.

所以你可以并排使用它们,但不能这样混合使用。

快速修复可能是:

auth = tweepy.OAuth1UserHandler(
    consumer_key, consumer_secret, access_token, access_token_secret
)

api = tweepy.API(auth)

public_tweets = api.home_timeline()

for tweet in public_tweets:
    print(tweet.text)