您可以使用 Ruby 中的 Net::HTTP 发出 YouTube 分析 API 请求吗?

Can you make a YouTube analytics API request using Net::HTTP in Ruby?

假设我的 access_token 和我的 TEST_CHANNEL_ID 是正确的,这个请求不应该有效吗?

我收到 404 错误,我是否缺少某些参数?

uri = URI.parse("https://www.googleapis.com")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Post.new("/youtube/analytics/v1/reports")
request.body = URI.encode_www_form({"Authorization" => "Bearer #{access_token}", "ids" => "channel==#{TEST_CHANNEL_ID}", "start-date" => "2015-07-01", "end-date" => "2015-07-20", "metrics" => "views"})

response = http.request(request)

值得一提的是,我的 access_token 获得了 refresh_token 所有 youtube api 范围的授权

如果您将访问令牌作为 URL 参数传递,则参数名称应为 "access_token" 并且您不需要字符串的 "Bearer" 部分;它只是:

request.body = URI.encode_www_form({"access_token" => "#{access_token}", "ids" => "channel==#{TEST_CHANNEL_ID}", "start-date" => "2015-07-01", "end-date" => "2015-07-20", "metrics" => "views"})

如果您在请求中设置访问令牌,则只能使用 Authorization: Bearer {whatever} 表单 header。

Here's the documentation 就此.