Search code examples
pythongitlab-apipython-gitlab

Getting HTTP Error 403 Forbidden when making call to GitLab server using Python code


I have written a small Python code to get the Storage usage of a GitLab repository. However when I run my code then I get this 403 Forbidden error. This repo, say test_repo_1 is under a test_group_1. I have created the token with Maintainer role both at project level and at the group level (group access token) and used them separately in my Python script individually to run the code but get the same result as 403.

Python code:-

import gitlab

GITLAB_URL = "https://gitlab.com"

GL_GROUP_TOKEN = '<Group token & Project access token>'

def grant_access(gl_project_id):
    gl = gitlab.Gitlab(GITLAB_URL, private_token = GL_GROUP_TOKEN)
    project = gl.projects.get(gl_project_id)    
    storage = project.storage.get()
    print("storage::",storage)

def main():
    gl_project_id='8724648'    
    role_requested=''
    grant_access(gl_project_id)

main()

Error log:-

python3 storage.py
Traceback (most recent call last):
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/exceptions.py", line 336, in wrapped_f
    return f(*args, **kwargs)
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/mixins.py", line 154, in get
    server_data = self.gitlab.http_get(self.path, **kwargs)
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/client.py", line 829, in http_get
    "get", path, query_data=query_data, streamed=streamed, **kwargs
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/client.py", line 797, in http_request
    response_body=result.content,
gitlab.exceptions.GitlabHttpError: 403: 403 Forbidden

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "storage.py", line 20, in <module>
    main()
  File "storage.py", line 18, in main
    grant_access(gl_project_id)
  File "storage.py", line 12, in grant_access
    storage = project.storage.get()
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/v4/objects/projects.py", line 1257, in get
    return cast(ProjectStorage, super().get(**kwargs))
  File "/home/nairv/.local/lib/python3.7/site-packages/gitlab/exceptions.py", line 338, in wrapped_f
    raise error(e.error_message, e.response_code, e.response_body) from e
gitlab.exceptions.GitlabGetError: 403: 403 Forbidden

Group access token scopes:- enter image description here

Project access token scopes:- enter image description here


Solution

  • Documentations says This endpoint requires admin access. https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html#project-storage