简体   繁体   English

如何使用Faraday gem从Api检索数据

[英]How to retrieve data from an Api using Faraday gem

Am trying to access data from Exact Online API Using Faraday Gem and it is returning the status of "401" meaning am not authorized to access that link before I login, but I had already been authenticated and I logged in successfully in a different page. 我正在尝试使用Faraday Gem从Exact Online API中访问数据,并且返回状态"401"这意味着在我登录之前未经授权可以访问该链接,但是我已经通过身份验证,并且已成功在其他页面上登录。

If I try to access that link individually via the browser URL bar I can see the data. 如果尝试通过浏览器URL栏单独访问该链接,则可以看到数据。

Here is my controller 这是我的控制器

 def example1
    @exact_conn = Faraday.new(:url => 'https://start.exactonline.nl') do |data|
      data.request :url_encoded
      data.response :logger
      data.adapter Faraday.default_adapter
    end

   data = @exact_conn.get("/api/v1/{division}/salesinvoice/SalesInvoices"
   @received_data = data.status
  end

my View 我的观点

<h1>Received Data</h1>
<%= @received_data %>

How can i solve this issue? 我该如何解决这个问题?
Or Is their any other option I can use in order to access that data directly without being authenticated for second time?. 还是我可以使用它们的任何其他选项来直接访问该数据而无需再次进行身份验证?

For APIs there is often authentication required on each request in the form of a token passed in your HTTP headers. 对于API,通常需要以HTTP头中传递的令牌的形式对每个请求进行身份验证。 If that is how this API works, you may try something like: 如果这是该API的工作方式,则可以尝试执行以下操作:

@exact_conn.get do |req|
  req.url('/api/v1/{division}/salesinvoice/SalesInvoices')
  req.headers['Authorization'] = "Token token=#{your_token_here}"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM