【问题标题】:Autheticate Github private repo in python在 python 中验证 Github 私有仓库
【发布时间】:2020-05-27 15:25:43
【问题描述】:

我想验证给定的 repo URL、用户 ID 和密码组合是否有效。 我正在为此使用请求。下面是我的python代码:

requests.get('https://github.com/geetikatalreja/WebApp_DotNet.git', auth = ('valid_username', 'Valid_password'))

requests.get('https://github.com/geetikatalreja/WebApp_DotNet.git', auth=HTTPBasicAuth('valid_username', 'Valid_password'))

这两个语句都返回错误代码 401。当需要身份验证并且已失败或尚未提供身份验证时,会出现错误代码 401,但我能够使用来自 Github UI 的相同凭据和 URL 登录。

请提供帮助。

【问题讨论】:

  • 我认为错误 401 表示未授权...我认为您需要以不同方式进行身份验证
  • 我已经编辑了这个问题..你能分享一些其他的身份验证方式吗。

标签: python github python-requests python-3.7


【解决方案1】:

如果您使用 GitHub API,但您不能将 Web UI 与基本身份验证一起使用,则这种身份验证有效。

通常,Web UI 使用登录表单,在您登录时发送 POST 请求。之后,使用会话 cookie 以保持登录状态(用于会话)。如果在会话过期后登录仍然存在,则网站可以使用持续更长时间的 cookie。我认为 GitHub 使用了这个概念。

我建议您将 API 用于自动化流程,因为您可以更轻松地解析响应。

另外,我强烈建议不要对真实密码使用基本身份验证。我会改用 PAT。

如果您想向 API 发送经过身份验证的请求,您可以例如执行

requests.get('https://api.github.com/repos/geetikatalreja/WebApp_DotNet', auth = ('valid_username', 'Valid_password'))

除了密码,您也可以只使用您帐户的 PAT(这样更安全)。您可以创建 PAT over there

GitHub API 文档可以在 here 找到,用于访问存储库的文档可以在 there 找到。

【讨论】:

  • 您能否分享一些有关如何验证 Web UI 的链接或代码。我是 Python 新手,对此一无所知。
  • 很简单:打开浏览器并进行身份验证。对于任何自动访问,请使用 API。
【解决方案2】:

您可以传入application/vnd.github.VERSION.diff 媒体类型来获取差异。 这样就可以了

requests.get('https://api.github.com/geetikatalreja/WebApp_DotNet.git/:owner/:repo/pulls/:number', auth = ('valid_username', 'Valid_password'))

格式应该是这样的

requests.get('https://api.github.com/repos/:owner/:repo/pulls/:number', auth = ('valid_username', 'Valid_password'), headers=headers)

在哪里

headers = {
    'Authorization': 'token mygithubtoken',
    'Accept': 'application/vnd.github.VERSION.diff',
}

我们无法使用 OAuth 令牌访问该网站。但是,可以通过 API 获得差异:

https://developer.github.com/v3/pulls/#get-a-single-pull-request

【讨论】:

    【解决方案3】:

    改用基于令牌的身份验证。这是从github获取个人令牌的链接

    https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line

    一旦你有了令牌,你就可以 -

    1. 从命令行登录
        $ git clone https://github.com/geetikatalreja/WebApp_DotNet.git
        Username: your_username
        Password: your_token
    
    1. 使用代码登录(Python urllib2 模块)
        url = "https://api.github.com/geetikatalreja/WebApp_DotNet.git/:owner/:repo/pulls/:number"
        token = "your_token"
    
        request = Request(url)
        request.add_header('Authorization', 'token %s' % token)
        response = urlopen(request) 
    
    1. 使用python请求模块登录
        import requests
        url = "https://github.com/geetikatalreja"
        response = requests.get(url, headers={'Authorization': 'your_token'})
    

    但由于某种原因,如果您必须使用用户名/密码登录,那么您可以使用以下代码

    免责声明:代码未经测试,复制自here。此代码不使用 API 而是解析网页并进行登录

        s = requests.Session()
        r = s.get('https://www.github.com/login')
        tree = html.fromstring(r.content)
        data = {i.get('name'):i.get('value') for i in tree.cssselect('input')}
        data['login'] = username
        data['password'] = password
        r = s.post('https://github.com/session', data=data)
    

    【讨论】:

    • 我想使用用户名和密码进行授权。要求只是验证用户名、密码和 URL
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2020-06-17
    • 2020-03-12
    • 2021-04-09
    • 2021-06-30
    相关资源
    最近更新 更多