让我的 python 代码与 Twitter 交互以更新我的状态的问题

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

努力让我的非常基本的更新状态代码正常工作!这里是新手,所以对我要轻松一些!

import tweepy
import os

def main():
    # Get your secrets from Replit secrets
    consumer_key = os.environ['API_KEY']
    consumer_secret = os.environ['API_KEY_SECRET']
    access_token = os.environ['ACCESS_TOKEN']
    access_token_secret = os.environ['ACCESS_TOKEN_SECRET']

    # 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)

    # The text to be tweeted
    tweet_text = "Good morning, Twitter!"

    # Posting the tweet
    try:
        api.update_status(status=tweet_text)
        print("Tweet successfully posted:", tweet_text)
    except tweepy.TweepError as e:
        print("Error sending tweet:", e)

if __name__ == "__main__":
    main()

错误代码:

Traceback (most recent call last):
 File "main.py", line 25, in main api.update_status(status=tweet_text)
 File " /home/runner /twitter/venv/lib/python3.10/site-packages/tweepy/api.py", line 46, in wrapper
    return method(*args, **kwargs)
 File " /home/runner/twitter/venv/lib/python3.10/site-packages/tweepy/api.py", line 979, in update_ status
    return self. request
 File "/home/runner/twitter/venv/lib/python3.10/site-packages/tweepy/api.py", line 271, in request
    raise Forbidden(resp)
      tweepy.errors.Forbidden: 403 Forbidden
      453 - You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may n eed a different access level. You can learn more here: https://developer.twitter.com/en/ portal/product

During handling of the above exception, anot her exception occurred:

Traceback (most recent call last):
 File "main.py", line 31, in <module> main
 File "main.py", , line 27, in main except tweepy. TweepError as e:
    AttributeError: module 'tweepy' has no attri bute 'TweepError'

就我的 Twitter 应用程序而言,该应用程序具有读写权限,并且我处于免费状态(我正在试验时的基本层)

python twitter tweepy
1个回答
0
投票

您似乎正在尝试调用Twitter API v1.1,它只能用于媒体上传

对于推文创建,您必须调用Twitter API v2

替换:

# 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)

# The text to be tweeted
tweet_text = "Good morning, Twitter!"

# Posting the tweet
try:
    api.update_status(status=tweet_text)
    print("Tweet successfully posted:", tweet_text)
except tweepy.TweepError as e:
    print("Error sending tweet:", e)

使用以下代码片段:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=consumer_key,
    consumer_secret=consumer_secret,
    access_token=access_token,
    access_token_secret=access_token_secret
)

# The text to be tweeted
tweet_text = "Good morning, Twitter!"

# Posting the tweet
try:
    client.create_tweet(text=tweet_text)
    print("Tweet successfully posted:", tweet_text)
except Exception as e:
    print("Error sending tweet:", e)

发布带有图像的推文:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=consumer_key,
    consumer_secret=consumer_secret,
    access_token=access_token,
    access_token_secret=access_token_secret,
)
auth = tweepy.OAuth1UserHandler(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret,
)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)

# The text to be tweeted
tweet_text = "Good morning, Twitter!"

# Upload image
media = api.media_upload("tweet_img.png")

# Post tweet with image
try:
    client.create_tweet(text=tweet_text)
    print("Tweet successfully posted:", tweet_text)
except Exception as e:
    print("Error sending tweet:", e)

欲了解更多信息:

https://developer.twitter.com/en/docs/twitter-api

https://docs.tweepy.org/en/stable/client.html#tweepy.Client.create_tweet

我希望这有帮助。

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