我正在尝试使用 Python 验证 GitHub 凭据。
我已经尝试过这个:
import urllib2, base64
username = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="780c1d0b0c381d00191508141d561b1715" rel="noreferrer noopener nofollow">[email protected]</a>"
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"
但它总是返回成功
,即使密码错误。我做错了什么?
最佳答案
换行
request = urllib2.Request("https://github.com/")
至
request = urllib2.Request("https://api.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
关于python - 检查 GitHub 凭证有效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634656/