【问题标题】:Why does github3.py ask for a second authentication factor twice?为什么 github3.py 两次要求第二个身份验证因素?
【发布时间】:2016-05-22 13:07:45
【问题描述】:

我正在使用 github3.py 访问我组织的 Github 帐户,并且我们启用了双因素身份验证。我首先列出存储库。代码如下:

import os

import github3

USER = os.environ['GITHUB_USERNAME']
PASS = os.environ['GITHUB_PASSWORD']

try:
    # Python 2
    prompt = raw_input
except NameError:
    # Python 3
    prompt = input

def get_second_factor():
    print("Authenticator called")
    code = ''
    while not code:
        # The user could accidentally press Enter before being ready
        code = prompt('Enter 2FA code: ')
        print("Received code:", code)
    return code

gh = github3.login(USER, PASS, two_factor_callback=get_second_factor)

org = gh.organization("<ORGNAME>")
for repo in org.iter_repos(type="all"):
    print(repo.ssh_url)

不幸的是,对github3.login 的调用似乎不仅触发了对第二个因素的请求,对org.iter_repos 的调用还触发了第二个请求。

这是预期的行为吗?如何确保程序仅在第一次需要时尝试 2FA?

【问题讨论】:

    标签: python two-factor-authentication github3.py


    【解决方案1】:

    所以简短的回答是,为了避免需要输入第二因素代码,您可以通过访问网站上的设置来创建“个人访问令牌”。当你拥有它时,你可以将它作为token 参数传递给github3.login

    长答案是调用login 不会调用它,而是调用organizationiter_repos 会触发两个2FA 请求。事实上,如果你有超过 100 个存储库,那么之后每一百个存储库都会要求你提供它。原因是 GitHub API 会在您每次向 API 发出请求时提示您输入第二因素令牌。这意味着每次 github3.py 向 API 发出请求时,我们都会收到一个特定的响应,这是一个挑战。发生这种情况时,我们会使用您输入的令牌重复请求。因此,为了避免多次输入,您需要使用个人访问令牌。

    【讨论】:

    • 谢谢,我试试看
    • 优秀 - 无需提供用户名和密码
    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2016-06-06
    • 2016-02-21
    相关资源
    最近更新 更多