Python 2.7.6中的身份验证

Python 2.7.6中的身份验证,python,python-2.7,Python,Python 2.7,我很难弄清楚如何对github的登录进行身份验证以获取XML或JSON格式的数据。这是我现在掌握的代码: import urllib2, sys, re, base64 url = "https://api.github.com/user" username = "username" password = "password" passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, u

我很难弄清楚如何对github的登录进行身份验证以获取XML或JSON格式的数据。这是我现在掌握的代码:

import urllib2, sys, re, base64
url = "https://api.github.com/user"
username = "username"
password = "password"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url).read()
我一直在修补我发现的这段代码,但我似乎总是收到一个错误,上面写着“urllib2.HTTPError:HTTP error 401:Unauthorized”


我确信我的登录信息是正确的,我不明白为什么会出现这个错误

这一行看起来错误
pass.add\u password…
pass
在python中是一个保留字。它应该是
passman。添加密码(无、url、用户名、密码)
可能会有帮助:对不起,输入错误,应该是passman