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

rest
streaming
python
twitter4j
api

#1

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”,
http://twitter.com/statuses”, “username”, “password”)
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {‘status’: msg} )
resp = urllib.request.urlopen(“http://twitter.com/statuses/update.json”, params)
resp.read()


#2

Me 2, It doesn’t work.


#3

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.


#4

Thank you very much, Abraham! I will check out


#5

My favorite is python-twitter


#6

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