YouTube上传v3 – 400错误请求

huangapple go评论82阅读模式
英文:

YouTube Upload v3 - 400 Bad Request

问题

我注意到在尝试通过以下链接上传视频时,我遇到了相当多的HTTP 400错误:
https://developers.google.com/youtube/v3/docs/videos/insert

我正在使用来自Google的Go SDK:
code.google.com/p/google-api-go-client

这些上传失败的视频有一个共同点,就是在视频的片段数据(标题/描述)中存在类似<>的字符。如果我删除冲突的字符,视频就可以成功上传。

我似乎在文档中找不到相关信息,但是我是否需要进行某种净化处理?HTML转义?删除所有非^A-Za-z0-9的内容?那么非HTML中使用的<,比如<3呢?Unicode字符呢?我感到困惑。

编辑:
为了回答我的问题,我写了一个小技巧来解决Google不喜欢><字符的问题。我只是用看起来相似的不同UNICODE字符替换它们。

// < 和 > 需要被去除,否则上传会抛出400错误
// 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
}
英文:

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
}

答案1

得分: 1

一个问题是:

> 这些是从Google Discovery Service的JSON描述文件中自动生成的Go库,用于可用的“新样式”Google API。
>
> 公告邮件:
> http://groups.google.com/group/golang-nuts/browse_thread/thread/6c7281450be9a21e
>
> 状态:相对于其他Google API客户端,此库被标记为alpha版。某些高级功能可能无法正常工作。如果发现任何问题,请报告错误。

由于它们是从JSON服务定义自动生成的,它们可能错过了适当的转换为Go语言的部分。根据API文档的假设,假设您正在使用http协议,视频信息将作为JSON blob发送。

Go语言将为您转换特殊字符。因此,<>等字符将变为JSON有效的Unicode转义序列。Google可能不喜欢转义序列,所以您可以尝试发送字面字符。但我真的怀疑这是问题所在。

另外,由于您提到<> youtube不允许您输入HTML,所以如果您正在进行这样的操作,或者输入的内容看起来像HTML,那可能是无效字符错误的原因。您需要对任何看起来像HTML的内容进行清理。

请参考这篇帖子:

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

这篇文章展示了Golang生成Unicode转义序列的示例:

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

英文:

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

huangapple
  • 本文由 发表于 2014年6月6日 14:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/24075229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定