如何解决我的 Tweepy 代码中的 403 Forbidden 错误?

问题描述 投票:0回答:1
import tweepy, codecs

consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

Bearer_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

api.update_status("Hello from Python for Udemy Data Science and Machine Learning Course")

错误

Forbidden                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 api.update_status("Hello from Python for Udemy Data Science and Machine Learning Course")

File ~\anaconda3\lib\site-packages\tweepy\api.py:46, in payload.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
     44 kwargs['payload_list'] = payload_list
     45 kwargs['payload_type'] = payload_type
---> 46 return method(*args, **kwargs)

File ~\anaconda3\lib\site-packages\tweepy\api.py:979, in API.update_status(self, status, **kwargs)
    976 if 'media_ids' in kwargs:
    977     kwargs['media_ids'] = list_to_csv(kwargs['media_ids'])
--> 979 return self.request(
    980     'POST', 'statuses/update', endpoint_parameters=(
    981         'status', 'in_reply_to_status_id',
    982         'auto_populate_reply_metadata', 'exclude_reply_user_ids',
    983         'attachment_url', 'media_ids', 'possibly_sensitive', 'lat',
    984         'long', 'place_id', 'display_coordinates', 'trim_user',
    985         'card_uri'
    986     ), status=status, **kwargs
    987 )

File ~\anaconda3\lib\site-packages\tweepy\api.py:271, in API.request(self, method, endpoint, endpoint_parameters, params, headers, json_payload, parser, payload_list, payload_type, post_data, files, require_auth, return_cursors, upload_api, use_cache, **kwargs)
    269     raise Unauthorized(resp)
    270 if resp.status_code == 403:
--> 271     raise Forbidden(resp)
    272 if resp.status_code == 404:
    273     raise NotFound(resp)

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 need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

这个错误如何解决?我研究了代码并观看了视频,但错误没有解决。

python json python-3.x tweepy
1个回答
1
投票

Twitter 已逐步淘汰 API v1,因此您需要使用 v2。所以对于 tweepy 你可以使用 tweepy.Client(v2) 而不是 tweepy.API(v1) 因此,转换为 tweepy.Client 版本时,您的代码将如下所示。

import tweepy

consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Bearer_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

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

client.create_tweet(text="Hello from Python for Udemy Data Science and Machine Learning Course")

这将会像你期望的那样工作。

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