My script consumes gitlab API using certificate. When I run the code below, I got this error, ssl verification error:
requests.exceptions.SSLError: HTTPSConnectionPool(host='gitlab-pprd.oncf.net', port=443): Max retries exceeded with url: /api/v4/projects (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)')))
An error occurred: HTTPSConnectionPool(host='gitlab-pprd.oncf.net', port=443): Max retries exceeded with url: /api/v4/projects (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)')))
This is my script:
# consumer/management/commands/consume_gitlab_api.py
import logging
import requests
from django.core.management.base import BaseCommand
import certifi
class Command(BaseCommand):
def handle(self, *args, **kwargs):
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info("Starting the consume_gitlab_api management command...")
gitlab_api_url = 'https://gitlab-pprd.net/api/v4/projects'
headers = {
'PRIVATE-TOKEN': ''
}
ca_cert_path = 'C://Users//user//Desktop//consumeapi//gitlab-pprd.net.cert.pem'
try:
logger.debug("Making request to GitLab API...")
response = requests.get(gitlab_api_url, headers=headers, verify=ca_cert_path)
logger.debug(f"Received response: {response.status_code}")
if response.status_code == 200:
self.stdout.write(self.style.SUCCESS('Successfully fetched data from GitLab'))
self.stdout.write(str(response.json())) # Convert to string to avoid printing errors
logger.info("Successfully fetched data from GitLab")
else:
self.stderr.write(self.style.ERROR('Failed to fetch data from GitLab'))
self.stderr.write(f'Status code: {response.status_code}')
self.stderr.write(f'Response: {response.text}')
logger.error("Failed to fetch data from GitLab. Status code: %s, Response: %s", response.status_code, response.text)
except Exception as e:
logger.exception("An error occurred while fetching data from GitLab: %s", e)
self.stderr.write(self.style.ERROR(f"An error occurred: {e}"))
I tried changing certificates, work with sessions nothing happens.
New contributor
Reda BAOUSSOUS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.