Search code examples
python-3.xyoutubeyoutube-data-api

How to grant scope to also upload captions on YouTube


I have a Python script that uploads videos and thumbnails, following the documentation, and it works. I want to adapt it to also upload captions or subtitles. Following this gist, it seems easy:

youtube = get_authenticated_service(args)

def upload(youtube, filepath, language, video_id):
  youtube.captions().insert(
    part="snippet",
    body={
      "snippet": {
        "videoId": video_id,
        "language": language,
        "name": "Subtitles"
      }
    },
    media_body=filepath
  ).execute()

This code fails with this error:

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "errors": [
      {
        "message": "Insufficient Permission",
        "domain": "global",
        "reason": "insufficientPermissions"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

So I try to authorize my app to have permission for subtitles. [The documentation] suggests these scopes:

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope

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

I don't think I can grant myself status as a YouTube partner, and I don't see what SSL has to do with it.

The Google Cloud Console dashboard for my app shows no results for "scopes" when I search for it on the top bar. When I search for "OAuth", I see the pages for Enabled APIs and services and Credentials, but I don't see scopes.

How can I enable an app that already uploads videos and thumbnails to also upload subtitles?


Solution

  • Based on the API documentation, at least one of those scopes you listed are required:

    Scope

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

    First, add them to your project. Open your OAuth consent screen and in the second page you will find the scopes. Search for youtube and you will find both scopes in there as in the following screenshot. Got this screenshot from one of my projects here

    Then, you also need to include them in your code. For example, you'll need to change the tutorial to add these scopes to the scope argument:

    def get_authenticated_service_for_subtitles(args):
      flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
        scope=[
         "https://www.googleapis.com/auth/youtube.upload",
         "https://www.googleapis.com/auth/youtube.force-ssl",
         "https://www.googleapis.com/auth/youtubepartner",  # Optional, for channels you don't own.
        ],
        message=MISSING_CLIENT_SECRETS_MESSAGE)
    

    As you mentioned, you don't need the youtubepartner scope if you are adding the captions to your own channel. If you add to channels that you manage, then you do need the youtubepartner scope.