如何在Rails中运行cURL命令

问题描述 投票:0回答:2

我正在使用Ruby on Rails 5,并且需要在我的应用程序中执行以下命令:

curl -F 'client_id=126581840734567' -F 'client_secret=678ebe1b3b8081231aab27dff738313' -F 'grant_type=authorization_code' -F 'redirect_uri=https://uri.com/' -F 'code=AQBi4L2Ohy3Q_N3V48OygFm0zb3gEsL985x5TIyDTNDJaLs93BwXiT1tyGYWoCg1HlBDU7ZRjUfLL5HVlzw4G-7YkVEjp6Id2WuqOz0Ylt-k2ADwDC5upH3CGVtHgf2udQhLlfDnQz5NPsnmxjg4bW3PJpW5FaQs8fn1ztgYp-ssfAf6IRt2-sI45ZC8cqqr5K_12y0Nq_Joh0H-tTfVyNLKatIxHPCqRDb3tfqgmxim1Q' https://api.instagram.com/oauth/access_token

以便它返回类似:

{"access_token": "IGQVJYS0k8V6ZACRC10WjYxQWtyMVRZAN8VXamh0RVBZAYi34RkFlOUxXZnTJsbjlEfnFJNmprQThmQ4hTckpFUmJEaXZAnQlNYa25aWURnX3hpO12NV1VMWDNMWmdIT3FicnJfZAVowM3VldlVWZAEViN1ZAidHlyU2VDMUNuMm2V", "user_id": 17231445640157812}

有没有一种方法可以使Rails执行这些类型的命令?我正在尝试以下:

uri = URI.parse('https://api.instagram.com/oauth/access_token')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({
  "client_id" => "126581840734567",
  "client_secret" => "678ebe1b3b8081231aab27dff738313",
  "grant_type" => "authorization_code",
  "redirect_uri" => "http://nace.network/",
  "code" => params[:code]
  })    


res = Net::HTTP.start(uri.hostname, uri.port) do |http|
  http.request(request)
end

但出现以下错误:

end of file reached

在此行:

res = Net::HTTP.start(uri.hostname, uri.port) do |http|
  http.request(request)
end
ruby-on-rails ruby instagram-api
2个回答
2
投票
您正在使用HTTPS,因此需要将其添加到您的代码中:

Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| res = http.request(request) end

但是如果您不需要持久连接,也可以使用此:

res = Net::HTTP.post_form(uri, "client_id" => "126581840734567", "client_secret" => "678ebe1b3b8081231aab27dff738313", "grant_type" => "authorization_code", "redirect_uri" => "http://nace.network/", "code" => params[:code] )

此外,您可以考虑使用像Faraday这样的库,它更容易处理。

0
投票
Curl是从命令行发出HTTP(或HTTP)请求的一种方法。

您不想在Rails中使用CURL。您想从Rails内部发出HTTP请求。

我们可以进一步细化为您想要从Ruby发出HTTP请求。缩小/提炼出问题的最基本版本总是好的。

我们可能已经知道了这一切-仍然值得我们所有人受益!

在Ruby中使用HTTP

我们要使用HTTP客户端。有很多但是,为此,我将使用Faraday(宝石),因为我喜欢它。

[您使用Ruby内置的NET:HTTP开了一个不错的开端,但我更喜欢Faraday的DSL。这将导致代码更具可读性和可扩展性。

所以,这是一堂课!因此,我几乎没有对此进行测试,以此作为起点。确保为此编写了一些单元测试。

# This is a Plain Old Ruby Object (PORO) # It will work in Rails but, isn't Rails specific. require 'faraday' # This require is needed as it's a PORO. class InstagramOAuth attr_reader :code # The code parameter will likely change frequently, so we provide it # at run time. def initialize(code) @code = code end def get_token connection.get('/oauth/access_token') do |request| request.params[:code] = code end end private def connection @connection ||= Faraday.new( url: instagram_api_url, params: params, ssl: { :ca_path => https_certificate_location } ) end def instagram_api_url @url ||= 'https://api.instagram.com' end # You need to find out where these are for your self. def https_certificate_location '/usr/lib/ssl/certs' end def params # These params likely won't change to often so we set a write time # in the class like this. { client_id: '126581840734567', client_secret: '678ebe1b3b8081231aab27dff738313', grant_type: 'authorization_code', redirect_uri: 'https://uri.com/' } end end # How do we use it? Like so # Your big old authorisation code from your question code = 'AQBi4L2Ohy3Q_N3V48OygFm0zb3gEsL985x5TIyDTNDJaLs93BwXiT1tyGYWoCg1HlBDU'\ '7ZRjUfLL5HVlzw4G-7YkVEjp6Id2WuqOz0Ylt-k2ADwDC5upH3CGVtHgf2udQhLlfDnQz'\ '5NPsnmxjg4bW3PJpW5FaQs8fn1ztgYp-ssfAf6IRt2-sI45ZC8cqqr5K_12y0Nq_Joh0H'\ '-tTfVyNLKatIxHPCqRDb3tfqgmxim1Q' # This will return a Faraday::Response object but, what is in it? response = InstagramOAuth.new(code).get_token # Now we've got a Hash response_hash = response.to_hash puts 'Request made' puts "Request full URL: #{response_hash[:url]}" puts "HTTP status code: #{response_hash[:status]}" puts "HTTP response body: #{response_hash[:body]}"

当我运行上面的代码段时,我得到了以下内容。该类有效,您只需调整请求参数,直到获得所需的内容。希望该类演示如何在Ruby / Rails中发送HTTP请求。

Request made Request full URL: https://api.instagram.com/oauth/access_token?client_id=126581840734567&client_secret=678ebe1b3b8081231aab27dff738313&code=AQBi4L2Ohy3Q_N3V48OygFm0zb3gEsL985x5TIyDTNDJaLs93BwXiT1tyGYWoCg1HlBDU7ZRjUfLL5HVlzw4G-7YkVEjp6Id2WuqOz0Ylt-k2ADwDC5upH3CGVtHgf2udQhLlfDnQz5NPsnmxjg4bW3PJpW5FaQs8fn1ztgYp-ssfAf6IRt2-sI45ZC8cqqr5K_12y0Nq_Joh0H-tTfVyNLKatIxHPCqRDb3tfqgmxim1Q&grant_type=authorization_code&redirect_uri=https%3A%2F%2Furi.com%2F HTTP status code: 405 HTTP response body:

附加阅读

https://lostisland.github.io/faraday/usage/

https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates

© www.soinside.com 2019 - 2024. All rights reserved.