Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

go - YouTube上传v3-400错误请求(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

(我注意到,尝试通过以下方式上传视频时,我收到了大量HTTP 400错误: 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

(我正在使用Google的Go SDK: 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?

(HTML转义?)

Removal of everything thats ^A-Za-z0-9 ?

(删除所有多数民众赞成^A-Za-z0-9吗?)

What about non-html usage of < , like <3 ?

(<非html用法怎么样,例如<3呢?)

What about unicode characters?

(Unicode字符呢?)

I'm confused.

(我糊涂了。)

EDIT: To answer my question, here is a little hack I wrote to combat the issue of Google hating on > , < characters.

(编辑:要回答我的问题,这是我写的一个小技巧,用于解决><字符上的Google仇恨问题。)

I simply replace them with different UNICODE characters that look similar.

(我只是用看起来相似的不同UNICODE字符替换了它们。)

// < 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
}
  ask by if __name__ is None translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(这些是从可用的“新型” Google API的Google Discovery Service的JSON描述文件中自动生成的Go库。)

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

(公告电子邮件: http : //groups.google.com/group/golang-nuts/browse_thread/thread/6c7281450be9a21e)

Status: Relative to the other Google API clients, this library is labeled alpha.

(状态:相对于其他Google API客户端,该库被标记为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.

(由于它们是根据JSON服务定义自动生成的,因此可能错过了适当的翻译。)

From the API documentation, assuming you are using the http protocol, the video information is sent as a JSON blob.

(从API文档中,假设您使用的是http协议,则视频信息将作为JSON Blob发送。)

Go will convert special characters for you.

(Go将为您转换特殊字符。)

So <>, etc become JSON valid unicode escape sequences.

(因此<>等成为JSON有效的Unicode转义序列。)

Google may dislike escape sequences so you might want to try sending literal characters.

(Google可能不喜欢转义序列,因此您可能想尝试发送文字字符。)

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.

(另外,由于您提到<>,因此youtube不允许您输入HTML,因此如果您正在执行的操作或类似html的操作,则可能是无效字符错误的原因。)

You will need to sanitize anything that looks like html.

(您将需要清理所有类似于html的内容。)

See this post:

(看到这篇文章:)

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

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

This shows golang generates unicode escape sequences:

(这表明golang会生成unicode转义序列:)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...