如何在ruby中指定youtube api的时间

时间:2017-07-27 06:54:52

标签: ruby api youtube

我正在编写Ruby脚本与Youtube Streaming API进行通信以创建直播和直播。到目前为止,我已经能够编写插入新流的代码,但它可以正常工作但插入广播时我遇到了问题。我收到以下错误:

/usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.8.0/lib/google/api_client.rb:652:in `block (2 levels) in execute!': Scheduled start time is required (Google::APIClient::ClientError)
    from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.8.0/lib/google/api_client.rb:635:in `block in execute!'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable'
    from /usr/local/rvm/gems/ruby-2.3.0/gems/google-api-client-0.8.0/lib/google/api_client.rb:626:in `execute!'
    from ruby_script.rb:44:in `insert_broadcast'
    from ruby_script.rb:98:in `<main>'

所以,它抱怨我没有提到预定的开始时间。这是相关的代码:

# Create a liveBroadcast resource and set its title, scheduled start time,
# scheduled end time, and privacy status.
def insert_broadcast(client, youtube, options)
  insert_broadcast_response = client.execute!(
    api_method: youtube.live_broadcasts.insert,
    parameters: {
      part: 'snippet,status'
    },
    body_object: {
      snippet: {
        title: options[:broadcast_title],
        scheduledStartTime: options[:state_time],
        scheduledEndTime: options[:end_time]
      },
      status: {
        privacyStatus: options[:privacy_status]
      }
    }
  )

  p "Broadcast: #{insert_broadcast_response.data.id}"
  return insert_broadcast_response.id
end

options = {
  stream_title: 'stream',
  broadcast_title: 'dumdum',
  start_time: '2018-01-30T00:00:00.000Z',
  end_time: '2018-01-30T00:01:00.000Z',
  privacy_status: 'private'
}

我借用Python中的this代码示例借用的日期时间格式。我尝试使用start_time: Time.now希望Ruby日期时间对象可能有效,但事实并非如此。我该如何解决这个问题?

修改Here's完整的脚本。

2 个答案:

答案 0 :(得分:2)

根据@ Simple-Lime的回答修复拼写错误,如果仍然无法解决问题,请尝试以下操作:

您需要正确格式化时间: Time.now输出2017-07-27 07:35:38 +0000,这是错误的。 您需要使用Time#iso8601将时区的十进制表示法更改为ISO-8601标准的日期时间格式:

require 'time'

# example time format: '2014-01-30T00:00:00.000Z'
Time.now.getutc.iso8601(3)
# => "2017-07-27T07:30:33.742Z"

答案 1 :(得分:1)

在您的代码中,您有scheduledStartTime: options[:state_time],并将其作为options[:start_time]

传递给方法