我用Java编写了一个程序,用于下载YouTube视频的隐藏式字幕:

YouTube.Captions.List request = youtube.captions()
            .list("snippet", videoId).setKey(apiKey);
        CaptionListResponse response = request.execute();

        List<Caption> captions = response.getItems();

        for (Caption caption : captions) {
            CaptionSnippet snippet = caption.getSnippet();
            System.out.println("ID: " + caption.getId());
            System.out.println("Name: " + snippet.getName());
            System.out.println("Language: " + snippet.getLanguage());

            String captionID = caption.getId();
            OutputStream output = new FileOutputStream("captions"+captionID);

            YouTube.Captions.Download req = youtube.captions()
                .download(captionID).setKey(apiKey);
            req.getMediaHttpDownloader();
            req.executeMediaAndDownloadTo(output);
        }


但是req.executeMediaAndDownloadTo(output);调用将引发以下异常:

There was an IO error: null : 401 Unauthorized
Login Required


难道我做错了什么?为什么要授权下载隐藏式字幕?特别要考虑到可以通过Youtube本身免费获得隐藏式字幕的事实:

- "..." below the youtube player
- Open transcript

最佳答案

根据the docs,访问Captions.list端点需要满足以下条件:


授权书

此请求需要具有以下至少一个范围(read more about authentication and authorization)的授权。

范围

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


same requirement也适用于Captions.download端点。

07-24 19:36