Post tweet with tweepy
Asked Answered
S

4

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?

Somali answered 12/10, 2013 at 18:25 Comment(0)
M
15

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

Mckeehan answered 12/10, 2013 at 18:35 Comment(2)
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?Floruit
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 @Floruit said, you will be issued new tokens reflecting the change in permissions for the associated account.Cornstalk
F
3

the code works for me with only

api.update_status (**status** = 'Updating using OAuth authentication via Tweepy!')
Fourcycle answered 20/8, 2015 at 21:49 Comment(1)
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.Deodorant
P
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.

Pennyroyal answered 16/3, 2022 at 8:33 Comment(0)
L
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 ([email protected])")

# 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()
Lamonicalamont answered 2/7, 2022 at 6:54 Comment(2)
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.Nudibranch
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.Nudibranch

© 2022 - 2025 — McMap. All rights reserved.