【问题标题】:How to get authenticated API request limits from the GitHub API using Ruby如何使用 Ruby 从 GitHub API 获取经过身份验证的 API 请求限制
【发布时间】:2022-01-26 06:16:00
【问题描述】:

我正在尝试更新一个旧的 Ruby 应用程序,该应用程序正在使用 GitHub API 使用的 recently (2021-08-08) Deprecated 身份验证方法,以便能够减少限制要求。在 URL 中提供 OATH2 身份验证令牌之前,但现在必须在请求标头中提供它们。

我尝试了几种方法,但由于我不是 Ruby 程序员,我无法解决问题。

使用curl 执行此操作很简单,并且可以在 GitHub 文档页面中找到示例。

# Simple, should give a rate limit of: 60
curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/rate_limit

# Authenticated, should give a rate limit of: 5000
curl -H "Accept: application/vnd.github.v3+json" -H "Authorization: token <MY-40-CHAR-TOKEN>" https://api.github.com/rate_limit

使用 Ruby 是另一回事,因为从那时起,Ruby 的许多事情似乎也发生了变化。


require 'net/http'

#-----------------------------------------------------
# Test-1
#-----------------------------------------------------
uri = URI("https://api.github.com/rate_limit")
params = { :Authorization => "token <MY-40-CHAR-TOKEN>", :Accept => "application/vnd.github.v3+json" }
uri.query = URI.encode_www_form(params)

res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)


#-----------------------------------------------------
# Test-2
#-----------------------------------------------------
uri = URI.parse("https://api.github.com/rate_limit")
http = Net::HTTP.new(uri.host, uri.port)

req = Net::HTTP::Get.new(uri.request_uri)
req["User-Agent"] = "my-ruby-script"
req["Accept"] = "application/vnd.github.v3+json" 
req["Authorization"] = "token <MY-40-CHAR-TOKEN>" 

res = http.request(req)
res["content-type"]

response.each_header do |key, value|
  p "#{key} => #{value}"
end

puts res.body


#-----------------------------------------------------
# Test-3
#-----------------------------------------------------
uri = URI.parse('https://api.github.com/rate_limit')
http = Net::HTTP.new(uri.host, uri.port)
res = http.request_get(uri, 'User-Agent': 'myscript' , 'Accept': 'application/vnd.github.v3+json', 'Authorization': 'token <MY-40-CHAR-TOKEN>')


那些都给出了无用的错误...... 然后有人发帖说你可能需要使用:

Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https')

我要开始工作的代码是:


#-----------------------------------------------------
# Test-4
#-----------------------------------------------------
require 'net/http'
require 'json'

hLine = "-"*60

    if (1)
        access_token = "<MY-40-CHAR-TOKEN>"

        if (access_token)
            puts "Using access_token: #{access_token}" 
            
            # :headers => { "Authorization" => "token " + access_token },
            uri = URI.parse("https://api.github.com/rate_limit")
            req = Net::HTTP::Get.new(uri)
       
            req["User-Agent"] = "myscript"
            req["Accept"] = "application/vnd.github.v3+json" 
            req["Authorization"] = "token #{access_token}" 
            res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https')   
        else 
            uri = URI.parse("https://api.github.com/rate_limit")
        end

        res = Net::HTTP.get_response(uri) 
        
        if (res.message != "OK")    # 200
            puts "ERROR: Bad response code: #{res.code}\n"
            puts res.body
        else
            debug = 1
            if (debug)
                puts hLine + "\nResponse Headers:\n" + hLine
                puts "#{res.to_hash.inspect}\n"
                puts hLine + "\nBody:\n" + hLine
                puts "#{res.body}\n" + hLine
            end

            rbr = JSON.parse(res.body)['resources']['core']
            RTc = Time.at(rbr['reset'])
            puts "\nCore"
            puts "  Rate limit   : #{rbr['limit']}"
            puts "  Remaining    : #{rbr['remaining']}"
            puts "  Refresh at   : #{RTc}"

        end
    end


# OUTPUT:
#----------------------------------------------
# Core
#  Rate limit   : 60
#  Remaining    : 59
#  Refresh at   : 2022-01-25 20:40:52 +0200

所以令牌永远不会被应用。

整个程序也在使用oktokit,所以使用它的解决方案也可能有用。

如何通过新的 GitHub API 更改使其在 Ruby 中工作?

【问题讨论】:

    标签: ruby oauth-2.0 github-api


    【解决方案1】:

    这个问题似乎更多地与糟糕的 Ruby 文档维护有关,它似乎大多被过时的解决方案所掩盖。另一个最常见的问题是,在我给出(发现)的示例中,它们从未正确处理SSL(HTTPS)。没有 SSL 就没有成功的 GitHub 交互。另外声明他们也不接受请求头中的空User-Agent

    正确(高度简化)的解决方案如下:

    require 'net/http'    
    
    url = URI("https://api.github.com/rate_limit")
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    
    req = Net::HTTP::Get.new(url)     
    req["Authorization"] = 'token <MY-40-CHAR-TOKEN>'
    res = http.request(req) 
    
    puts res.read_body
    

    在适当的地方替换 OP 中的代码可以解决这个问题。


    为了将来参考,如果您需要将curl 转换为Ruby 的net/http 格式,请使用此站点:

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2018-02-19
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 2022-08-14
      • 2013-02-09
      相关资源
      最近更新 更多