我正在尝试使用http://developer.github.com/v3/来检索项目问题。这有效:
curl -u "Littlemaple:mypassword" https://api.github.com/repos/MyClient/project/issues
它返回我客户项目的所有私人问题。但是,我无法找到如何在 Python 中实现它。我发现的两种方法(例如Python urllib2 Basic Auth Problem)都不起作用,它们返回 404 或 403 错误:
def fetch(url, username, password):
"""Wonderful method found on forums which does not work.""""
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(url)
f = urllib2.urlopen(req)
return f.read()
...和:
def fetch(url, username, password):
"""Wonderful method found on forums which does not work neither.""""
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(request).read()
有任何想法吗?提前致谢!