Skip to content

Add 'verify' parameter to control SSL verification behavior #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

marcelodiaz558
Copy link

Description

This PR adds a new verify parameter to control SSL certificate verification, providing both API and CLI interfaces for this functionality. This offers a quick solution for common SSL-related issues that users have reported.

Problem Solved

This change provides an immediate solution for users experiencing SSL-related issues like:

Instead of requiring users to modify environment variables or implement complex workarounds, they can now directly control SSL verification behavior through both the API and CLI.

Changes

  • Added verify parameter to get_transcript(), get_transcripts(), and list_transcripts() methods
  • Added --verify option to CLI with support for both custom certificate paths and disabling verification
  • Added comprehensive test coverage for both API and CLI functionality
  • Added documentation in README.md with examples for both API and CLI usage
  • Default value is None to maintain backward compatibility
  • Parameter is passed through to the underlying requests session

The implementation follows the same pattern as Python requests' verify parameter, making it familiar to users who work with the requests library.

Usage

# API usage
from youtube_transcript_api import YouTubeTranscriptApi

# Using custom CA bundle
transcript = YouTubeTranscriptApi.get_transcript(video_id, verify='/path/to/cacert.pem')

# Disabling SSL verification (not recommended for production)
transcript = YouTubeTranscriptApi.get_transcript(video_id, verify=False)
# CLI usage
# Using custom certificate bundle
youtube_transcript_api video_id --verify /path/to/cacert.pem

# Disabling SSL verification
youtube_transcript_api video_id --verify False

@giltotherescue
Copy link

I just ran into this problem and would love to see this code merged

@psaks
Copy link

psaks commented Jan 30, 2025

I just ran into this problem and would love to see this code merged

Same here - this functionality is essential! However, in the meantime you can hack it by creating your own YouTubeTranscriptApi class (far from ideal, but it works :)

@jdepoix
Copy link
Owner

jdepoix commented Mar 11, 2025

Hi all!
sorry that it took so long, but I didn't want to merge this, as I had some larger-scale changes to the API in mind, which I just got around to releasing in v1.0.0!

In this new version an instance of YouTubeTranscriptApi can be initialized with a requests.Session object. This allows for specifying the CA bundle file, but at the same time allows for doing other things like settings custom headers!

from requests import Session

http_client = Session()

# set custom header
http_client.headers.update({"Accept-Encoding": "gzip, deflate"})

# set path to CA_BUNDLE file
http_client.verify = "/path/to/certfile"

ytt_api = YouTubeTranscriptApi(http_client=session)
ytt_api.fetch(video_id)

# share same Session between two instances of YouTubeTranscriptApi
ytt_api_2 = YouTubeTranscriptApi(http_client=session)
# now shares cookies with ytt_api
ytt_api_2.fetch(video_id)

Since this new API allows for setting CA bundles without requiring an extra param for it, I will close this PR now. Nonetheless, thank you very much for your contribution @marcelodiaz558! 😊🙏

@jdepoix jdepoix closed this Mar 11, 2025
@marcelodiaz558
Copy link
Author

Hi all! sorry that it took so long, but I didn't want to merge this, as I had some larger-scale changes to the API in mind, which I just got around to releasing in v1.0.0!

In this new version an instance of YouTubeTranscriptApi can be initialized with a requests.Session object. This allows for specifying the CA bundle file, but at the same time allows for doing other things like settings custom headers!

from requests import Session

http_client = Session()

# set custom header
http_client.headers.update({"Accept-Encoding": "gzip, deflate"})

# set path to CA_BUNDLE file
http_client.verify = "/path/to/certfile"

ytt_api = YouTubeTranscriptApi(http_client=session)
ytt_api.fetch(video_id)

# share same Session between two instances of YouTubeTranscriptApi
ytt_api_2 = YouTubeTranscriptApi(http_client=session)
# now shares cookies with ytt_api
ytt_api_2.fetch(video_id)

Since this new API allows for setting CA bundles without requiring an extra param for it, I will close this PR now. Nonetheless, thank you very much for your contribution @marcelodiaz558! 😊🙏

Awesome work @jdepoix! Thanks for taking the time to implement this and for the clear example. I've tested v1.0.3 using the http_client argument with the verify attribute set, and it's working perfectly on my end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants