Getting Basecamp API Working with Python
I found
one library that was linked everywhere, but it wasn’t working for me. I was always getting 400 Bad Request when using it. Chris Conover was able to get the following code working.
import urllib2 protocol = 'https://' url = 'example.com' command = '/projects.xml' headers = {'Accept' : 'application/xml', 'Content-type' : 'applications/xml'} username = 'x' password = 'y' # Setup password stuff passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) # Send the request and get response req = urllib2.Request(protocol + url + command, None, headers) response = urllib2.urlopen(req) results = response.read() print results
I thought it was a problem with how the authorization was formed so based on the above code I modified the old basecamp.py file and I was able to get a response. The following is what I changed.
Around line 64
def __init__(self, username, password, protocol, url): self.baseURL = protocol+url if self.baseURL[-1] == '/': self.baseURL = self.baseURL[:-1] passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) self.opener = urllib2.build_opener(authhandler)
And around line 142
path = '/projects.xml'
With that I was able to use basecamp.py to retrieve a list of projects. Other modifications may be needed for other features, but that was all I planned on using.
Here is an example of using
ElementTree to parse the XML response to get the names of all of the projects returned from basecamp.
import elementtree.ElementTree as ET from basecamp import Basecamp protocol = 'https://' url = 'example.com' username = 'x' password = 'y' bc = Basecamp(username, password, protocol, url) projects = bc.projects() tree = ET.fromstring(projects) tags = tree.getiterator(tag='project') for t in tags: project_name = t.findtext('name')
Related posts:
- Python Projects in Users’ Home Directories with wsgi Letting users put static files and php files in a…
- How to Add Locations to Python Path for Reusable Django Apps In my previous post I talk about reusable apps, but…
- Setting up Apache2, mod_python, MySQL, and Django on Debian Lenny or Ubuntu Hardy Heron Both Debian and Ubuntu make it really simple to get…