【发布时间】: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