Skip to content

Instantly share code, notes, and snippets.

@alexdean
Last active September 30, 2020 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdean/70af1b9e8687c7435ea7ee8a2cfa7b03 to your computer and use it in GitHub Desktop.
Save alexdean/70af1b9e8687c7435ea7ee8a2cfa7b03 to your computer and use it in GitHub Desktop.
updating a youtube video with an invalid language code results in a deceptive invalidVideoMetadata error

overview

YouTube's API will return a confusing error code if you use a language code which YT does not support.

request

PUT https://www.googleapis.com/youtube/v3/videos?part=snippet&key={YOUR_API_KEY}

{
 "id": "GV40JxaAHdI",
 "snippet": {
  "title": "3 Days on the Missouri Riv",
  "categoryId": "27",
  "defaultLanguage": "nb"
 }
}

'nb' is 'Norwegian Bokmål'. Youtube only accepts 'no' for 'Norwegian'.

response

{
 "error": {
  "errors": [
   {
    "domain": "youtube.video",
    "reason": "invalidVideoMetadata",
    "message": "The request metadata is invalid.",
    "locationType": "other",
    "location": "body"
   }
  ],
  "code": 400,
  "message": "The request metadata is invalid."
 }
}

more info

YouTube's documentation on error codes is incomplete/deceptive in this case.

They say that invalidVideoMetadata means:

The request metadata is invalid. This error occurs if the request updates the snippet part of a video resource but does not set a value for both the snippet.title and snippet.categoryId properties.

In this request, I've provided proper values for those fields. The error is that the snippet contains an unrecognized language code.

updated request

to verify, i re-issued the same request, changing only the language code.

PUT https://www.googleapis.com/youtube/v3/videos?part=snippet&key={YOUR_API_KEY}

{
 "id": "GV40JxaAHdI",
 "snippet": {
  "title": "3 Days on the Missouri Riv",
  "categoryId": "27",
  "defaultLanguage": "en"
 }
}

resulting response

{
 "kind": "youtube#video",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/wjuLYtR1rIhaz9K9brVVJSpdwyg\"",
 "id": "GV40JxaAHdI",
 "snippet": {
  "publishedAt": "2015-07-09T16:03:09.000Z",
  "channelId": "UCEX9BscEMpJfaASiewenbYg",
  "title": "3 Days on the Missouri Riv",
  "description": "",
  "thumbnails": {
   "default": {
    "url": "https://i.ytimg.com/vi/GV40JxaAHdI/default.jpg",
    "width": 120,
    "height": 90
   },
   "medium": {
    "url": "https://i.ytimg.com/vi/GV40JxaAHdI/mqdefault.jpg",
    "width": 320,
    "height": 180
   },
   "high": {
    "url": "https://i.ytimg.com/vi/GV40JxaAHdI/hqdefault.jpg",
    "width": 480,
    "height": 360
   }
  },
  "channelTitle": "Alex Dean",
  "categoryId": "27",
  "liveBroadcastContent": "none",
  "defaultLanguage": "en",
  "localized": {
   "title": "3 Days on the Missouri Riv",
   "description": ""
  },
  "defaultAudioLanguage": "en"
 }
}

solution?

use only language codes supported by YouTube, which can be discovered via Youtube's i18nLanguages api

@apolopena
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment