正在通过 YouTube 数据下载 non-owned 视频的隐藏式字幕 API [Python]

Downloading closed captions of non-owned video through YouTube Data API [Python]

我正在使用 Python 编写请求视频隐藏式字幕的应用程序。 代码看起来像这样:

videoID = getVideo(videoURL)
request = youtube.videos().list(
    part="snippet,contentDetails,statistics",
    id=videoID
)
response = request.execute()
items = response.get("items")[0]
contentDetails = items["contentDetails"]
caption = contentDetails["caption"]

if(caption):
    print("Video contains closed captions!")
else:
    print("Video does not contain closed captions.")

#get caption info
if(caption):
    caption_info = youtube.captions().list(part='id', videoId=videoID).execute().get('items', [])
    caption_str = youtube.captions().download(id=caption_info[0]['id'], tfmt='srt').execute()

最后一行抛出 403 错误:

raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/captions/uQKrZZwFbPddlMeZauOtvq1sR61wb1UwuVB4yxq7798%3D?tfmt=srt returned "The permissions associated with the request are not sufficient to download the caption track. The request might not be properly authorized, or the video order might not have enabled third-party contributions for this caption.". Details: "[{'message': 'The permissions associated with the request are not sufficient to download the caption track. The request might not be properly authorized, or the video order might not have enabled third-party contributions for this caption.', 'domain': 'youtube.caption', 'reason': 'forbidden', 'location': 'id', 'locationType': 'parameter'}]"

我已经正确创建了 API 凭据和 OAuth 2.0 客户端 ID,并且可以成功获取视频信息,例如标题、频道名称、持续时间等。 但是,每当我使用上述代码请求字幕时,我都会收到该错误。我不拥有请求的视频。

有什么方法可以通过 YouTube 数据 API 下载我 拥有的视频的隐藏式字幕?

编辑 1: 这是处理 YouTube 数据 API 身份验证

的代码
SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"]

def youtube_authenticate():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "credentials.json"
    creds = None
    
    #check if authentication has already been completed
    if os.path.exists("token.pickle"):
        with open("token.pickle", "rb") as token:
            creds = pickle.load(token)
    #perform the authentication (1 time only)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(client_secrets_file, SCOPES)
            creds = flow.run_local_server(port=0)
        # save the authenticated credentials
        with open("token.pickle", "wb") as token:
            pickle.dump(creds, token)

    return build(api_service_name, api_version, credentials=creds)

根据 Captions.download 端点的官方规范,API 不允许下载非自有视频的字幕:

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scopes
https://www.googleapis.com/auth/youtube.force-ssl
https://www.googleapis.com/auth/youtubepartner