【问题标题】:Can you make a YouTube analytics API request using Net::HTTP in Ruby?您可以在 Ruby 中使用 Net::HTTP 发出 YouTube 分析 API 请求吗?
【发布时间】:2015-09-27 01:27:15
【问题描述】:

假设我的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 范围

【问题讨论】:

    标签: ruby youtube youtube-api youtube-data-api youtube-analytics-api


    【解决方案1】:

    如果您将访问令牌作为 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}form。

    Here's the documentation 在此。

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2018-01-14
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多