pythonauthenticationtwittercookielib

Twitter authentication using cookielib


Can someone tell me why this doesn't work?

import cookielib
import urllib
import urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
data = urllib.urlencode({'session[username_or_email]':'twitter handle' , 'session[password]':'password'})
opener.open('https://twitter.com' , data)
stuff = opener.open('https://twitter.com')
print stuff.read()

Why doesn't this give the html of the page after logging in?


Solution

  • Please consider using an Oauth library for your task. Scraping the site using mechanize is not recommended because twitter can change the HTML specific stuffs any time, and then your code will break.

    Check this out: Python-twitter at http://code.google.com/p/python-twitter/

    Simplest example to post an update:

    >>> import twitter
    >>> api = twitter.Api(
                consumer_key='yourConsumerKey',
                consumer_secret='consumerSecret',
                access_token_key='accessToken',
                access_token_secret='accessTokenSecret')
    >>> api.PostUpdate('Blah blah lbah!')
    

    There can be many reasons why it is failing: