我正在尝试使用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()
有什么想法吗?提前感谢!
-v
选项来窥视 curl 实际上正在做什么,以查看是否缺少任何重要的交互。这可能会为你提供一些线索,以更接近在 Python 中模拟其行为。 - SingleNegationElimination