rammen
rammen

Reputation: 23

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

I'm writing an application using Python that requests videos' closed captions. The code looks something like this:

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()

The last line throws a 403 error:

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'}]"

I have properly created the API credentials and OAuth 2.0 Client IDs and can successfully get video information such as title, channel name, duration etc. However whenever I request the captions using the above code I get that error. I do not own the requested videos.

Is there any way to download through the YouTube Data API the closed captions of a video I don't own?

EDIT 1: Here's the code that handles the YouTube Data API authentication

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)

Upvotes: 2

Views: 658

Answers (1)

stvar
stvar

Reputation: 6985

As per the official specification of the Captions.download endpoint, the API does not allow one to download captions of non-owned videos:

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

Upvotes: 2

Related Questions