0

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

2
  • is maintainer level access enough to obtain such information through UI or api? Commented Nov 28, 2023 at 7:40
  • I guess yes, I have given maintainer access only Commented Nov 28, 2023 at 13:16

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.