Twitter API 问题:

问题描述 投票:0回答:2

我在尝试运行 python 脚本以在我的帐户上重播推文时遇到以下错误。


import tweepy

#Consumer keys and access tokens, used for OAuth
API_KEY = “MY_API_KEY”
API_SECRET_KEY = “MY_API_SECRET_KEY”
ACCESS_TOKEN = “MY_ACCESS_TOKEN”
ACCESS_TOKEN_SECRET = “MY_ACCESS_TOKEN_SECRET”

#OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

#Creation of the actual interface, using authentication
api = tweepy.API(auth)

mentions= api.mentions_timeline()
for mention in mentions:
    print(str(mention.id)+" - "+ mention.text)
    print("heeloo")

主要思想是向任何使用 api.mentions_timeline() 提及我帐户的人重播

但我有问题“453 - 您当前只能访问 Twitter API v2 端点的子集和有限的 v1.1 端点(例如媒体发布、oauth)。如果您需要访问此端点,则可能需要不同的访问级别.您可以在这里了解更多信息:https://developer.twitter.com/en/portal/product

我也已经将我的帐户升级到基本每月 100 美元 谁能帮助我吗?谢谢

twitter bots tweepy
2个回答
1
投票

如果您想获取或发布推文,则必须使用 API 的 V2,而不是 V1。

对于 Tweepy,这意味着使用 Client 类以及 get_users_mentionscreate_tweet 方法。您可以在他们的网站上找到示例here(选择 API v2 选项卡)。

例如,如果您想发布推文:

import tweepy

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

client = tweepy.Client(
    consumer_key=consumer_key, consumer_secret=consumer_secret,
    access_token=access_token, access_token_secret=access_token_secret
)

# Create Tweet

# The app and the corresponding credentials must have the Write permission

# Check the App permissions section of the Settings tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps

# Make sure to reauthorize your app / regenerate your access token and secret 
# after setting the Write permission

response = client.create_tweet(
    text="This Tweet was Tweeted using Tweepy and Twitter API v2!"
)

print(f"https://twitter.com/user/status/{response.data['id']}")

或者,如果您想获得用户的提及:

user_id = 2244994945

response = client.get_users_mentions(user_id)

# By default, only the ID and text fields of each Tweet will be returned
for tweet in response.data:
    print(tweet.id)
    print(tweet.text)

# By default, the 10 most recent Tweets will be returned
# You can retrieve up to 100 Tweets by specifying max_results
response = client.get_users_mentions(user_id, max_results=100)

(您可能需要使用

client.get_users_mentions(user_id, user_auth=True)


0
投票

基本计划无法访问流媒体端点。适用于 Pro 计划及以上用户。更多信息请参阅产品文档

© www.soinside.com 2019 - 2024. All rights reserved.