简体   繁体   English

在 python 中验证 Github 私有仓库

[英]Autheticate Github private repo in python

I want to verify if the given combination of repo URL, userid and password is valid or not.我想验证给定的 repo URL、userid 和 password 组合是否有效。 I am using request for this.我正在为此使用请求。 Below is my python code:下面是我的python代码:

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

or或者

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

Both the statements are returning Error code 401. Error code 401 occurs when authentication is required and has failed or has not yet been provided, but I am able to login using the same credentials and URL from Github UI.这两个语句都返回错误代码 401。错误代码 401 在需要身份验证但已失败或尚未提供时发生,但我可以使用来自 Github UI 的相同凭据和 URL 登录。

Kindly assist.请协助。

This kind of authentication works if you use the GitHub API but you cannot use the Web UI with basic authentication.如果您使用 GitHub API,但无法使用具有基本身份验证的 Web UI,则此类身份验证有效。

Normally, a Web UI uses a login form that sends a POST Request when you log in. After that, the session cookie is used in order to stay logged in (for the session).通常,Web UI 使用登录表单,该表单在您登录时发送 POST 请求。之后,使用会话 cookie 以保持登录状态(对于会话)。 If the login should persist after the session expired, the website could use cookies that persist longer.如果在会话过期后登录仍然存在,则网站可以使用持续时间更长的 cookie。 I think GitHub uses this concept.我认为 GitHub 使用了这个概念。

I would recommend you to use the API for automated processes because you can parse the responses easier.我建议您将 API 用于自动化流程,因为您可以更轻松地解析响应。

Also, I strongly recommand not to use basic authentication with the real password.另外,我强烈建议不要使用真实密码进行基本身份验证。 I would use PATs instead.我会改用 PAT。

If you want to send authenticated requests to the API you can eg execute如果您想向 API 发送经过身份验证的请求,您可以执行

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

Instead of the password, you can also just use a PAT of your account(which is more secure).除了密码,您还可以使用您帐户的 PAT(这样更安全)。 You can create a PAT over there .你可以在那里创建一个 PAT。

The GitHub API documentation can be found here and the documentation for accessing repositories there . GitHub的API文档可以发现这里和访问存储库的文档存在

You can pass in the application/vnd.github.VERSION.diff media type to get the diff.您可以传入application/vnd.github.VERSION.diff媒体类型来获取差异。 So that would make it这样就可以了

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

the format should be like格式应该是这样的

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

where在哪里

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

We can't use OAuth tokens to access the website.我们无法使用 OAuth 令牌访问该网站。 However, diffs are available through the API:但是,差异可通过 API 获得:

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

Use token based authentication instead.请改用基于令牌的身份验证。 Here is the link to get the personal token from github这是从github获取个人令牌的链接

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

Once you have the token, you can -获得令牌后,您可以-

  1. Login from Command Line从命令行登录
    $ git clone https://github.com/geetikatalreja/WebApp_DotNet.git
    Username: your_username
    Password: your_token
  1. Login using Code (Python urllib2 module)使用代码登录(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. Login using python request module使用python请求模块登录
    import requests
    url = "https://github.com/geetikatalreja"
    response = requests.get(url, headers={'Authorization': 'your_token'})

BUT for some reason if you have to login using username/password then you can use below code但是出于某种原因,如果您必须使用用户名/密码登录,则可以使用以下代码

Disclaimer : code not tested, copied from here .免责声明:代码未经测试,从这里复制。 This code is not using API instead parsing web page and doing the login此代码不使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM