TWITTER API and urllib2

Mike 42flicks at gmail.com
Tue Jun 10 06:42:39 EDT 2008


Hello,

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. (
http://groups.google.com/group/twitter-development-talk/web/api-documentation#HelpMethods
).

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):
       pass

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

passwdMgr.add_password(None,netlock[1],authentication.username,authentication.password)

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

    def UserTimeline(self,authentication):
        self.url_request("http://twitter.com/statuses/user_timeline.xml
",authentication)

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

p = TwitterMethods()
auth = TwitterAuth('email at gmail.com','password')
p.UserTimeline(auth)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080610/4b66936f/attachment.html>


More information about the Python-list mailing list