11

I'm trying to post a tweet with the tweepy library. I use this code:

import tweepy

CONSUMER_KEY ="XXXX"
CONSUMER_SECRET = "XXXX"   
ACCESS_KEY = "XXXX"    
ACCESS_SECRET = "XXXX"

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

api = tweepy.API(auth)
api.update_status('Updating using OAuth authentication via Tweepy!')

But when I run the application, I receive this error:

raise TweepError(error_msg, resp)
TweepError: Read-only application cannot POST.

How can I fix this?

Liam
  • 6,009
  • 4
  • 39
  • 53
Riccardo Gai
  • 421
  • 1
  • 6
  • 19

4 Answers4

16

In the application's settings, set your Application Type to "Read and Write". Then renegotiate your access token.

user2737086
  • 309
  • 3
  • 6
  • What do you mean by renegotiate your access token? Do you need a new access token if the app was read only when you got the original token? – TedCap Oct 03 '14 at 03:23
  • I just figured out this part myself. After changing the application type, you must select *Regenerate My Access Token and Token Secret* for the account linked with your app. And, as @TedCap said, you will be issued new tokens reflecting the change in permissions for the associated account. – Shon Jan 31 '15 at 02:10
3

the code works for me with only

api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Marc
  • 31
  • 1
  • 2
    Actually this IS answer, just to a slightly different issue and it helped me, so I want to comment on this. I was getting a status response of '400' back from Twitter when I was using the Tweepy call: `api.update_status('Updating using OAuth authentication via Tweepy!')` but I was able to fix the issue by changing the method parameters to: `api.update_status(status = 'Updating using OAuth authentication via Tweepy!')` The Tweepy docs seem to be unclear about this nuance. – Adam Christianson Aug 28 '15 at 23:58
0

You have to set your app to read and write enter image description here

After that, you'll be able to run your code.

Ahmed Elgammudi
  • 642
  • 7
  • 15
0

the following python script will tweet a line from a text file. if you want to tweet multiple tweets just put them on a new line separated by a blank line.

import tweepy
from time import sleep
# import keys from cregorg.py
from credorg import *

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

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth, wait_on_rate_limit=True)

print("Tweet-TXT-Bot v1.0 by deusopus (deusopus@gmail.com)")

# Open text file tweets.txt (or your chosen file) for reading
my_file = open('tweets.txt', 'r')

# Read lines one by one from my_file and assign to file_lines variable
file_lines = my_file.readlines()

# Close file
my_file.close()

# Tweet a line every 5 minutes
def tweet():
    for line in file_lines:
        try:
             print(line)
             if line != '\n':
                 api.update_status(line)
                 sleep(300)
             else:
                pass
        except tweepy.errors.TweepyException as e:
            print(e)

while True:

    tweet()
deusopus
  • 3
  • 6
  • Although you example code is well commented, and may help others, it does not answer or explain the issue `TweepError: Read-only application cannot POST.` - the error raised by Tweepy or Twitter-API because of Twitter-account. – hc_dev Jul 03 '22 at 20:04
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – hc_dev Jul 03 '22 at 20:04