How do I send a message to Twitter using Python? This code does not work



I’m just a begginer at programming. I’ve been following a book, and I copy this code form there, but it doesn not work. Can anybody help me, pleasew

def send_to_twitter():
msg = "I am a message that will be sent to Twitter"
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password(“Twitter API”,”, “username”, “password”)
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
params = urllib.parse.urlencode( {‘status’: msg} )
resp = urllib.request.urlopen(“”, params)


Me 2, It doesn’t work.


That is very old code. The method of authentication and the API endpoint was removed several years ago. I recommend you check out the current list of available Python libraries and read up on the OAuth.


Thank you very much, Abraham! I will check out


My favorite is python-twitter


This topic was automatically closed after 24 hours. New replies are no longer allowed.