首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过YouTube数据API下载非拥有视频的封闭字幕[Python]

通过YouTube数据API下载非拥有视频的封闭字幕[Python]
EN

Stack Overflow用户
提问于 2021-09-04 10:55:37
回答 1查看 341关注 0票数 1

我正在使用Python编写一个应用程序,该应用程序请求视频的封闭标题。代码如下所示:

代码语言:javascript
运行
复制
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下载I不属于自己的视频的封闭式字幕?

编辑1:下面是处理YouTube数据API身份验证的代码

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-04 11:22:18

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

授权 此请求要求至少具有以下一个作用域(阅读有关身份验证和授权的更多信息。)的授权。 Scopes https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtubepartner

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69054635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档