skip to Main Content

I am just starting out with using Tweepy and have been having trouble when trying to use the v2 API functions to write a program to Tweet text. Currently, I am getting this error repeatedly:

Traceback (most recent call last):
  File "C:UsersxxxxxxxxPycharmProjectsTwitter BotTwitter Bot.py", line 12, in <module>
    response = client.create_tweet(text='Hello!')
  File "C:UsersxxxxxxxxPycharmProjectsTwitter Botvenvlibsite-packagestweepyclient.py", line 594, in create_tweet
    return self._make_request(
  File "C:UsersxxxxxxxxPycharmProjectsTwitter Botvenvlibsite-packagestweepyclient.py", line 118, in _make_request
    response = self.request(method, route, params=request_params,
  File "C:UsersxxxxxxxxPycharmProjectsTwitter Botvenvlibsite-packagestweepyclient.py", line 92, in request
    raise Forbidden(response)
tweepy.errors.Forbidden: 403 Forbidden

Here is my current code:

import tweepy
import config

client = tweepy.Client(
    consumer_key=config.api_key,
    consumer_secret=config.api_secret,
    access_token=config.access_token,
    access_token_secret=config.access_secret
)

response = client.create_tweet(text='Hello!')

print(response)

I have tried regenerating my tokens and keys a few times now, with similar results. I have also tried searching online for other users who have encountered this error, but haven’t had much luck finding anything besides others using v1 functions by mistake.

Is there anything I can do to fix this error?

2

Answers


  1. You most likely need to give your app write permission:

    Why am I encountering a 401 Unauthorized error with API or 403 Forbidden error with Client?

    If you’re using a method that performs an action on behalf of the authenticating user, e.g. API.update_status(), make sure your app has the write permission.

    After giving it the write permission, make sure to regenerate and use new credentials to utilize it.

    See Twitter’s API documentation on app permissions for more information.

    https://tweepy.readthedocs.io/en/v4.6.0/faq.html#why-am-i-encountering-a-401-unauthorized-error-with-api-or-403-forbidden-error-with-client

    Login or Signup to reply.
  2. Add permission to your application by including the user_auth=True parameter so that it looks something like this:

    import tweepy
    import config
    
    client = tweepy.Client(
        consumer_key=config.api_key,
        consumer_secret=config.api_secret,
        access_token=config.access_token,
        access_token_secret=config.access_secret,
        user_auth=True
    )
    
    response = client.create_tweet(text='Hello!')
    
    print(response)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search