0

Hello all I am pretty new to python and trying to debug this error below. The script basically creates a PAT (personal access token) on my Gitlab endpoint.

Traceback (most recent call last):
  File "/Test/test-gitlabtoken.py", line 86, in <module>
    main()
  File "/Test/test-gitlabtoken.py", line 79, in main
    name = sys.argv[1]
IndexError: list index out of range

Below is the full script I pulled from here --> https://github.com/vitalyisaev2/gitlab_token/blob/main/personal_access_token.py

#!/usr/bin/python3
"""
Script that creates Personal Access Token for Gitlab API;
Tested with:
- Gitlab Community Edition 10.1.4
- Gitlab Enterprise Edition 12.6.2
- Gitlab Enterprise Edition 13.4.4
"""
import sys
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

endpoint = "https://blah.com" # updated
root_route = urljoin(endpoint, "/")
sign_in_route = urljoin(endpoint, "/users/sign_in")
pat_route = urljoin(endpoint, "/-/profile/personal_access_tokens")

login = "root"
password = "pass" # updated


def find_csrf_token(text):
    soup = BeautifulSoup(text, "lxml")
    token = soup.find(attrs={"name": "csrf-token"})
    param = soup.find(attrs={"name": "csrf-param"})
    data = {param.get("content"): token.get("content")}
    return data


def obtain_csrf_token():
    r = requests.get(root_route)
    token = find_csrf_token(r.text)
    return token, r.cookies


def obtain_authenticity_token(cookies):
    r = requests.get(pat_route, cookies=cookies)
    soup = BeautifulSoup(r.text, "lxml")
    token = soup.find('input', attrs={'name': 'authenticity_token', 'type': 'hidden'}).get('value')
    return token


def sign_in(csrf, cookies):
    data = {
        "user[login]": login,
        "user[password]": password,
        "user[remember_me]": 0,
        "utf8": "✓"
    }
    data.update(csrf)
    r = requests.post(sign_in_route, data=data, cookies=cookies)
    token = find_csrf_token(r.text)
    return token, r.history[0].cookies


def obtain_personal_access_token(name, expires_at, csrf, cookies, authenticity_token):
    data = {
        "personal_access_token[expires_at]": expires_at,
        "personal_access_token[name]": name,
        "personal_access_token[scopes][]": "api",
        "authenticity_token": authenticity_token,
        "utf8": "✓"
    }
    data.update(csrf)
    r = requests.post(pat_route, data=data, cookies=cookies)
    soup = BeautifulSoup(r.text, "lxml")
    token = soup.find('input', id='created-personal-access-token').get('value')
    return token


def main():
    csrf1, cookies1 = obtain_csrf_token()
    print("root", csrf1, cookies1)
    csrf2, cookies2 = sign_in(csrf1, cookies1)
    print("sign_in", csrf2, cookies2)
    authenticity_token = obtain_authenticity_token(cookies2)

    name = sys.argv[1]
    expires_at = sys.argv[2]
    token = obtain_personal_access_token(name, expires_at, csrf2, cookies2, authenticity_token)
    print(token)


if __name__ == "__main__":
    main()

My python version is Python 3.9.7 I would appreciate any pointers. Thanks

Karl Diji
  • 103
  • 1
  • 6

2 Answers2

0

When searching for the error (source) line:

name = sys.argv[1]

you can find this: Meaning sys argv

The problem here is that the list given from sys.argv doesn't provide a second attribute.

If you are coding with Spyder IDE you may find some help to debug in the link: How to debug with Spyder in Python

Otherwise you might try to use the Python Debugger (Pdb).

Dharman
  • 30,962
  • 25
  • 85
  • 135
W4tu
  • 1
  • 2
0

Thanks for the feedback. I was missing some arguments from the command line. So I was able to unblock myself with this command below:

python3 personal_access_token.py mytoken 2022-08-27

mytoken --> token name 2022-08-27 --> given expiration date of the token

Karl Diji
  • 103
  • 1
  • 6