TWITTER API and urllib2

Mike 42flicks at
Tue Jun 10 12:42:39 CEST 2008


I've spent the last couple of nights hacking away at a Python wrapper for
the Twitter API that I can use for various things.

I'm having trouble with one of the methods: user_timeline. (

This is the only method that is returning a HTTP 401. It seems strange and
I'm not sure how to debug it further as the other methods requring
authentication work.

Please keep in mind the code is missing alot of polish :) - Though I'm open
to suggestions on improvements.

If anyone is familiar with this I'd really appreciate a hint as it has me
stumped! (I really need this method for my functionality too!)


import urllib2, urllib, urlparse

class TwitterMethods(object):
    def __init__(self):

    def url_request(self,uri,authentication=None):
        auth = urllib2.HTTPBasicAuthHandler()
        netlock  = urlparse.urlparse(uri)
        if authentication:
            passwdMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()


            auth = urllib2.HTTPBasicAuthHandler(passwdMgr)
        req = urllib2.Request(uri)
        o = urllib2.build_opener(auth)
            f =
            print f.readlines([0])
        except o.error:
            print "error"
        #    print "unknown error"

    def UserTimeline(self,authentication):

class TwitterAuth(object):
    def __init__(self,username,password):
        self.username = username
        self.password = password

p = TwitterMethods()
auth = TwitterAuth('email at','password')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list