403 Error with tweepy
Asked Answered
C

4

8

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

Cyclopedia answered 22/4, 2015 at 16:28 Comment(4)
twitter doesn't allows updating same status twice so try changing the status, also check the internet connection.Welty
@anmol_uppal It doesn't work with different string entries, but my internet connection is stable. However, perhaps it could be using some port that isn't open on my router? That's the only thing I could think of.Cyclopedia
No there is no port issue with it, then the only problem left is with your credentials, But you are saying that they are fine enough.Welty
@anmol_uppal They definitely are as it says on the page, unless I'm copying the wrong ones i.imgur.com/nZHrAlE.pngCyclopedia
F
3

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
Fauna answered 21/4, 2022 at 8:41 Comment(3)
i did it but still i got the same errorFingerbreadth
i tried all the search results that come up on google but nothing is working :( even i have elevated access, i am able to send update status to twitter but when i use filter or stream it will cause this errorFingerbreadth
@Aviroxi, I haven't seen your code. Maybe you are doing other things wrong. I don't have any problem streaming timeline.Fauna
N
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.

Niphablepsia answered 22/4, 2015 at 16:37 Comment(4)
Yes! I actually saw both of these 'solutions' but my clock is set accurately and adding the SSL makes no difference so doesn't seem to be the issue. Thank you, however!Cyclopedia
@Cyclopedia Instead of trying API.update_status(), try the API.me() and see if you still get the 403 Forbidden errorNiphablepsia
Ah! Well here's something interesting... File "C:\Python34\lib\site-packages\python_twitter-2.1-py3.4.egg\tweepy\auth.py", line 115, in get_username raise TweepError("Unable to get username, invalid oauth token!") tweepy.error.TweepError: Unable to get username, invalid oauth token! ------ Am i copying the wrong tokens? i.imgur.com/nZHrAlE.pngCyclopedia
Looks good to me. I'd try regenerating the consumer key and secret... It's a shot in the dark but it might workNiphablepsia
P
1

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.

Pfister answered 26/11, 2019 at 19:36 Comment(0)
M
1

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.

Marius answered 25/6, 2021 at 6:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.