goyoutubeyoutube-apigoogle-api-go-client

YouTube Upload v3 - 400 Bad Request


I've noticed I get quite a bit of HTTP 400 faliures when trying to upload videos via: https://developers.google.com/youtube/v3/docs/videos/insert

I am using the Go SDK from Google: code.google.com/p/google-api-go-client

The thing that the failed uploads have in common is that somewhere in the videos snippet data (title/description) there are characters like <, >. If I remove the conflicting character, the video uploads fine.

I can't seem to find it in documentation, but am I required to do some kind of sanitization? HTML escaping? Removal of everything thats ^A-Za-z0-9? What about non-html usage of <, like <3? What about unicode characters? I'm confused.

EDIT: To answer my question, here is a little hack I wrote to combat the issue of Google hating on >, < characters. I simply replace them with different UNICODE characters that look similar.

// < and > need to be stripped out, or the upload will throw 400 error
// https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_media:description
sanitize := func(val string) string {
    replacements := map[string]string{
        "<": "≺",
        ">": "≻",
    }
    for k, v := range replacements {
        val = strings.Replace(val, k, v, -1)
    }
    return val
}

Solution

  • One issue is:

    These are auto-generated Go libraries from the Google Discovery Service's JSON description files of the available "new style" Google APIs.

    Announcement email: http://groups.google.com/group/golang-nuts/browse_thread/thread/6c7281450be9a21e

    Status: Relative to the other Google API clients, this library is labeled alpha. Some advanced features may not work. Please file bugs if any problems are found.

    Since they are auto generated from the JSON service definition, they may have missed the appropriate translation to go. From the API documentation, assuming you are using the http protocol, the video information is sent as a JSON blob.

    Go will convert special characters for you. So <>, etc become JSON valid unicode escape sequences. Google may dislike escape sequences so you might want to try sending literal characters. But I really doubt that is the issue.

    Also, since you mention <> youtube won't let you put in HTML so if that is what you are doing, or something that looks like html, that could be the reason for your invalid character error. You will need to sanitize anything that looks like html.

    See this post:

    https://groups.google.com/forum/#!topic/youtube-api-gdata/EcYPPlHjllY

    This shows golang generates unicode escape sequences:

    http://play.golang.org/p/hv2h7PA0tr