'403 Error with tweepy

I am trying to use tweepy to operate a twitter account using Python but I appear to have slipped at the the first hurdle. No matter what I try, I keep getting a 403 error with no specific details.

import tweepy

# Consumer keys and access tokens, used for OAuth
consumer_key = 'XXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXX'
access_token = 'XXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXX'
 
 
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
 
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
 
# Sample method, used to update a status
api.update_status('Hello')
(keys are blanked out, but I assure you they are exactly as twitter provides)

This is the basic code provided by the tutorial, but it only returns

Traceback (most recent call last):
  File "C:\Users\Sam\Documents\Python\TWEEPY\Tweepy.py", line 22, in <module>
    api.update_status('Hello')
  File "C:\Python34\lib\site-packages\python_twitter-2.1-py3.4.egg\tweepy\binder.py", line 153, in _call
    raise TweepError(error_msg)
tweepy.error.TweepError: Twitter error response: status code = 403

Does anyone have any ideas? I can't see why I am forbidden from my request. The keys are not false as far as I can tell.

Thanks



Solution 1:[1]

This discussion is a bit old, but it might apply to you anyway.

They basically say that Tweepy uses HTTP by default and Twitter requires their API connections to go through TLS/SSL and suggest using auth = tweepy.auth.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, secure=True)

Also, one of the comments mentions having problems because his clock was set 10 minutes ahead of time.

Solution 2:[2]

the problem is that you have done already the tweet 'Hello' if you try to do something that has been done already you get this error.

Solution 3:[3]

The error 403 according to what I read in many places is related to authentication going wrong somewhere.

TL;DR I forgot to update my keys after changing access permissions to read and write from read on twitter's dev console.

In my case what had happened is initially I generated the keys on twitter's dev console but didn't see there was an option for access permissions. I saved the keys but then later realised about the access permissions thing so I changed it thinking it would be working. Then I got this error and was breaking my head as to what was going wrong so I thought maybe I had copied the keys wrong hence I went to regenerate then when I saw the access permissions button and it struck me that maybe the keys weren't updated with the new permissions i had set and that's what it turned out to be.

Solution 4:[4]

It seems that your app in Twitter Developer Portal does not have the required permissions to post a tweet.

To solve this problem:

  1. Go to Twitter Developer Portal
  2. Select your project
  3. Select your app
  4. In the settings tab scroll down and hit the edit button of User authentication settings

enter image description here

  1. Change App permissions to Read and write and Direct message

enter image description here

  1. Save
  2. Regenerate your Keys and Tokens

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Davide
Solution 2 Fernando Gardeazabal
Solution 3 Anoushk
Solution 4 Ehsan.R