我正在使用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下载I不属于自己的视频的封闭式字幕?
编辑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)
发布于 2021-09-04 11:22:18
根据Captions.download
端点的官方规范,API不允许下载非自有视频的标题:
授权 此请求要求至少具有以下一个作用域(阅读有关身份验证和授权的更多信息。)的授权。 Scopes
https://www.googleapis.com/auth/youtube.force-ssl
https://www.googleapis.com/auth/youtubepartner
https://stackoverflow.com/questions/69054635
复制相似问题