简体   繁体   English

如何使用 Ruby 中的承载令牌解析 Json

[英]How to Parse Json with Bearer Token in Ruby

I have recently graduated a Bootcamp and trying my luck in some projects and applying for jobs.我最近刚从训练营毕业,在一些项目中试试运气并申请工作。 Got a test, for this potential job, that I don't know how to tackle exactly.为这个潜在的工作做了一个测试,我不知道该如何解决。 At this stage, kind of just wondering how to solve the test.在这个阶段,有点想知道如何解决测试。

I need to create a website and display a list of all of the buildings, from this real estate company via their API.我需要创建一个网站并显示所有建筑物的列表,这些建筑物来自这家房地产公司,通过他们的 API。 They have provided me with the token for the access already.他们已经为我提供了访问令牌。

However, I'm not entirely sure how to connect with their API via Ruby.但是,我不完全确定如何通过 Ruby 与他们的 API 连接。 They gave me an example of how to connect but I haven't seen this before.他们给了我一个如何连接的例子,但我以前没见过。

curl -H "Authorization: Bearer token_they_provided" https://theirsite/api/buildings.

Any assistance to point me in the right direction will be appreciated.任何可以为我指明正确方向的帮助将不胜感激。

Thank you all!谢谢你们!

What you are trying to achieve is pretty much simple.您要实现的目标非常简单。 You can just add HTTP gem (there are other options, like Faraday gem ) to your project, which turns everything even simpler.您只需将HTTP gem (还有其他选项,例如Faraday gem )添加到您的项目中,这会使一切变得更加简单。

Using HTTP Gem使用 HTTP 宝石

  1. Add it to your Gemfile将其添加到您的 Gemfile
  2. Install using bundle使用捆绑安装
  3. Fetches API as below:获取 API 如下:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
  1. Parse JSON body:解析 JSON 主体:
JSON.parse(response.body)

You can read the gem docs for more information.您可以阅读 gem 文档以获取更多信息。 Also, feel free to connect me via LinkedIn另外,请随时通过 LinkedIn 与我联系

It's typically terrible exercise and can reason overall performance issues.这通常是一种可怕的运动,并且可能会导致整体性能问题。 We just had to get rid of a group of these on a mission because the processor become locking up我们只需要在一次任务中摆脱其中的一组,因为处理器被锁定

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

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