文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Getting Basecamp API Working with Python

Getting Basecamp API Working with Python

时间:2009-04-05  来源:cobrawgl

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')
排行榜 更多 +
随大同

随大同

冒险解谜 下载
弯曲路径:无尽奔跑

弯曲路径:无尽奔跑

休闲益智 下载
工艺融合对战

工艺融合对战

策略塔防 下载