You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS+Python3.12环境下PyTube访问streams触发AgeRestrictedError求助

解决PyTube开启OAuth后仍无法访问年龄限制视频的问题

针对macOS+Python3.12环境下,PyTube已设置use_oauth=True和allow_oauth_cache=True但仍触发AgeRestrictedError的问题,可按以下步骤排查解决:

  • 清除OAuth缓存文件
    PyTube的缓存可能未正确保存认证状态,删除缓存后重新认证:

    rm ~/.pytube/oauth.json
    

    重新运行代码,跟随提示完成谷歌账号授权。

  • 显式触发年龄验证逻辑
    在代码中主动调用bypass_age_gate(),确保认证会话被用于年龄验证请求:

    from pytube import YouTube
    
    yt = YouTube(
        "https://www.youtube.com/watch?v=G3e-cpL7ofc",
        use_oauth=True,
        allow_oauth_cache=True
    )
    yt.bypass_age_gate()
    print(yt.title)
    print(yt.streams)
    
  • 升级PyTube至最新版本
    Python3.12兼容性可能在旧版PyTube中存在问题,执行升级:

    pip install --upgrade pytube
    
  • 验证账号访问权限
    确保用于认证的谷歌账号在浏览器中可正常打开该年龄限制视频,排除账号本身无权限的情况。


问题详情

报错信息:

Traceback (most recent call last):
  File "/Volumes/Programmation/Python/Projects/Youtube Downloader/main.py", line 6, in <module>
    print(yt.streams)
          ^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pytube/__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
                       ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pytube/__main__.py", line 176, in fmt_streams
    stream_manifest = extract.apply_descrambler(self.streaming_data)
                                                ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pytube/__main__.py", line 160, in streaming_data
    self.bypass_age_gate()
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pytube/__main__.py", line 264, in bypass_age_gate
    raise exceptions.AgeRestrictedError(self.video_id)
pytube.exceptions.AgeRestrictedError: G3e-cpL7ofc is age restricted, and can't be accessed without logging in.

使用代码:

from pytube import YouTube

yt = YouTube("https://www.youtube.com/watch?v=G3e-cpL7ofc", use_oauth=True, allow_oauth_cache=True)
print(yt.title)
print(yt.streams)

内容的提问来源于stack exchange,提问作者Lounys

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:50:15