python - 检查 GitHub 凭证有效性

标签 python github urllib2

我正在尝试使用 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/

相关文章:

github - Github graphQL订购

github - 我可以拒绝 travis-ci 对组织的访问吗?

Git 从 TeamCity 9.0.3 构建推送

python - BeautifulSoup 使用 POST 表单操作解析 html 时遇到问题

python - 在Python 3.X中一次从url一行读取csv

python - 在 Python 3 中从网站中查找最常用的词

python - ruby 的 string inspect() 在 python 中的等价物是什么

Python:检查是否可以连接到给定的 url (urllib2)

python - 如何根据多个条件用字符串拆分 Pandas 数据框列

Python tkinter 框架对齐