【问题标题】:Ruby YouTube Data API v3 insert caption always returns errorRuby YouTube Data API v3 插入标题总是返回错误
【发布时间】:2022-07-30 00:12:42
【问题描述】:

我正在尝试使用 Ruby SDK 自动将视频上传到 YouTube。插入视频、删除视频和设置视频缩略图都可以正常工作,但由于某种原因,无论我使用什么参数,尝试添加字幕都会导致元数据客户端错误无效。

我编写了基于the documentation 的代码和其他语言的代码示例(我在当前的 gem 中找不到任何在 Ruby 中执行此操作的示例)。我正在使用google-apis-youtube_v3 gem,版本 0.22.0。

这是我的代码的相关部分(假设我上传了一个 ID 为 \'XYZ123\' 的视频):

require \'googleauth\'
require \'googleauth/stores/file_token_store\'
require \'google-apis-youtube_v3\'

def authorize [... auth code omitted ...] end

def get_service
  service = Google::Apis::YoutubeV3::YouTubeService.new
  service.key = API_KEY
  service.client_options.application_name = APPLICATION_NAME
  service.authorization = authorize
  service
end

body = {
  \"snippet\": {
    \"videoId\": \'XYZ123\',
    \"language\": \'en\',
    \"name\": \'English\'
  }
}

s = get_service
s.insert_caption(\'snippet\', body, upload_source: \'/path/to/my-captions.vtt\')

我尝试了许多不同的组合,但结果总是一样的:

Google::Apis::ClientError: invalidMetadata:请求包含无效的元数据值,这会阻止创建轨道。确认请求为snippet.languagesnippet.namesnippet.videoId 属性指定了有效值。 snippet.isDraft 属性也可以包括在内,但这不是必需的。状态码:400

看来语言和视频ID值真的没有太多选择,将字幕命名为“英文”也没什么了不起的。对于我传入的值可能有什么问题,我真的很茫然。

顺便说一句,即使我只是将 nil 作为 body 传递,我也会得到完全相同的响应。

    标签: ruby youtube-data-api google-api-ruby-client closed-captions


    【解决方案1】:

    我查看了 google-apis-youtube_v3 gem 中包含的 OVERVIEW.md 文件,它引用了Google simple REST client Usage Guide,它又提到大多数对象属性不使用驼峰式大小写(这是底层 JSON 表示使用的) .相反,在大多数情况下,必须使用 Ruby 的“snake_case”约定来发送属性。

    因此,sn-p 应该指定video_id 而不是videoId

    这似乎让请求通过,所以这解决了这个问题。

    我现在得到的响应状态为“失败”,失败原因为“处理失败”,但如果我无法弄清楚,这可能是另一个问题的主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2020-12-21
      • 2015-08-09
      • 2017-11-29
      • 2013-06-14
      • 1970-01-01
      • 2019-11-14
      • 2014-06-08
      相关资源
      最近更新 更多