你能在Ruby中使用.Net::HTTP制作YouTube分析API请求吗?



假设我的access_tokenTEST_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_tokenrefresh_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}表单。

这是相关文档

相关内容

  • 没有找到相关文章

最新更新