Python 检查GitHub凭据的有效性
我正在尝试用Python验证GitHub凭据 我试过这个:Python 检查GitHub凭据的有效性,python,github,urllib2,Python,Github,Urllib2,我正在尝试用Python验证GitHub凭据 我试过这个: import urllib2, base64 username = "test@example.com" password = "password" request = urllib2.Request("https://github.com/") base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') request.a
import urllib2, base64
username = "test@example.com"
password = "password"
request = urllib2.Request("https://github.com/")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
if result.code == 200:
print "Success"
else:
print "Error"
但它总是返回
Success
,即使密码错误也是如此。我做错了什么?我应该使用https://api.github.com/user
而不是https://github.com/
然而,我将使用请求
第三方库,这使得代码简洁:
import requests
print requests.get(
'https://api.github.com/user',
auth=('username', 'password')
)
换线
request = urllib2.Request("https://github.com/")
到
尝试更改您的凭据。它对我有用
测试
~/ $ python2 --version
Python 2.7.6
~/ $ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
~/ $ python2 --version
Python 2.7.6
~/ $ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux