'Publishing youtube video from draft in Youtube Data api v3

When manually uploading video however not configuring it, it will become draft.

I know Photos are not recommended but for here I show it for illustration. image

A file of the repository github.com/youtube/api-samples/blob/master/python/update_video.py couldn't be used since "the video couldn't be found error" will appear

Is there any API that I could use to publish the draft video to publically visible?



Solution 1:[1]

It looks like all uploads get added to an upload playlist whose id you can query. Here's a short example stub that works for me:

    import google_auth_oauthlib.flow
    import googleapiclient.discovery
    import googleapiclient.errors

    scopes = ["https://www.googleapis.com/auth/youtube.readonly",
            "https://www.googleapis.com/auth/youtube",
            "https://www.googleapis.com/auth/youtube.force-ssl"]

    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = YOUR_CLIENT_FILEPATH_HERE

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
    credentials = flow.run_local_server(port=0)

    youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)

    # get upload playlist id
    request = youtube.channels().list(
        part="contentDetails",
        mine=True
    )
    response = request.execute()
    uploads = response["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"]

    # get all uploaded videos
    request = youtube.playlistItems().list(
            part="snippet,contentDetails,status",
            playlistId=uploads,
            maxResults=50,
        )
    response = request.execute()
    allvids = response["items"]

    while "nextPageToken" in response.keys():
        request = youtube.playlistItems().list(
            part="snippet,contentDetails,status",
            playlistId=uploads,
            maxResults=50,
            pageToken = response["nextPageToken"]
        )
        response = request.execute()
        allvids += response["items"]

    # identify only private videos
    privvids = [vid for vid in allvids if vid["status"]["privacyStatus"] == "private"]

    # let's publish the oldest uploaded draft
    vid = privvids[-1]

    snippet = vid["snippet"]
    # modify snippet as needed

    # note that you must set a categoryId if updating the snippet
    snippet["categoryId"] = 27 #this is education

    # change status to public
    status = vid["status"]
    status["privacyStatus"]="public"
    status["selfDeclaredMadeForKids"] = False #not srictly required

    # apply changes
    response = youtube.videos().update(
        part='snippet,status',
        body=dict(
            snippet=snippet,
            status=status,
            id=snippet["resourceId"]["videoId"]
        )).execute()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dsavransky