0

我正在使用以下python代码作为python-gitlab库中用户名和密码的替代方法,该库充当GitLab REST API的包装器以登录GitLab:

import re
import sys

import requests

import gitlab


URL = 'https://gitlab.com'
SIGN_IN_URL = 'https://gitlab.com/users/sign_in'
LOGIN_URL = 'https://gitlab.com/users/sign_in'

session = requests.Session()
sign_in_page = session.get(SIGN_IN_URL).content
# Next line in the code causes the error described below!
for l in sign_in_page.split('\n'):
    m = re.search('name="authenticity_token" value="([^"]+)"', l)
    if m:
        break

token = None
if m:
    token = m.group(1)

if not token:
    print('Unable to find the authenticity token')
    sys.exit(1)

data = {'user[login]': 'login_or_email',
        'user[password]': 'SECRET',
        'authenticity_token': token}
r = session.post(LOGIN_URL, data=data)
if r.status_code != 200:
    print('Failed to log in')
    sys.exit(1)

gl = gitlab.Gitlab(URL, api_version=4, session=session)

我在第 16 行收到以下错误代码:

Traceback (most recent call last):
  File "C:/Users/saurav/AppData/Local/Programs/Python/Python38-32/gitlab_auth_code.py", line 16, in <module>
    for l in sign_in_page.split('\n'):
TypeError: a bytes-like object is required, not 'str'

'\n'下一行字符的使用似乎存在一些问题。我该如何修正这个错误?此代码实际上是在官方 python-gitlab 文档中提供的,作为使用 python 请求的解决方法,我相信它应该可以工作:https ://gist.github.com/gpocentek/bd4c3fbf8a6ce226ebddc4aad6b46c0a

任何帮助将非常感激!

4

0 回答 0