在rails上的ruby中为httparty添加api密钥

时间:2022-12-08 23:17:43

I am trying to use the adafruit api to access my data.

我正在尝试使用adafruit api来访问我的数据。

Currently when I try and access I get the following message:

目前,当我尝试访问时,我收到以下消息:

{"error":"not found - API documentation can be found at https://io.adafruit.com/api/docs"}

{“error”:“找不到 - 可以在https://io.adafruit.com/api/docs找到API文档”}

I think this is because I haven't added the key (as I get the same message if you don't use a key on the adafruit example page) but I don't know where to add it.

我想这是因为我没有添加密钥(因为如果你没有在adafruit示例页面上使用密钥,我得到相同的消息)但我不知道在哪里添加它。

Currently, I just have this in my controller:

目前,我只是在我的控制器中有这个:

def index

   @tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data')

end

Thanks in advance x

在此先感谢x

2 个解决方案

#1


1  

Please change your code with

请更改您的代码

def index

   @tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data?key_id=your_key_id')

end

#2


0  

According to the documentation at https://learn.adafruit.com/adafruit-io/browser the parameter for the key is X-AIO-Key. From what I can tell you can use a query param or header so the following combinations should work, in theory, I personally haven't tried them.

根据https://learn.adafruit.com/adafruit-io/browser上的文档,密钥的参数是X-AIO-Key。从我可以告诉你可以使用查询参数或标题,以便以下组合应该工作,理论上,我个人没有尝试过。

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', :headers => { 'content-type': 'application/json', 'X-AIO-Key': 'yourkey' })

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', {:headers => { 'content-type': 'application/json'}, :query => {'X-AIO-Key': 'yourkey' }})

There is also a ruby client library that uses the v1 api at https://github.com/adafruit/io-client-ruby. There is a pull request for v2 that hasn't been merged yet but that library uses faraday instead of httparty.

还有一个ruby客户端库,它使用https://github.com/adafruit/io-client-ruby上的v1 api。有一个尚未合并的v2拉取请求,但该库使用法拉第而不是httparty。

#1


1  

Please change your code with

请更改您的代码

def index

   @tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data?key_id=your_key_id')

end

#2


0  

According to the documentation at https://learn.adafruit.com/adafruit-io/browser the parameter for the key is X-AIO-Key. From what I can tell you can use a query param or header so the following combinations should work, in theory, I personally haven't tried them.

根据https://learn.adafruit.com/adafruit-io/browser上的文档,密钥的参数是X-AIO-Key。从我可以告诉你可以使用查询参数或标题,以便以下组合应该工作,理论上,我个人没有尝试过。

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', :headers => { 'content-type': 'application/json', 'X-AIO-Key': 'yourkey' })

@tests = HTTParty.get('https://io.adafruit.com/api/v2/username/myfeed/test/data', {:headers => { 'content-type': 'application/json'}, :query => {'X-AIO-Key': 'yourkey' }})

There is also a ruby client library that uses the v1 api at https://github.com/adafruit/io-client-ruby. There is a pull request for v2 that hasn't been merged yet but that library uses faraday instead of httparty.

还有一个ruby客户端库,它使用https://github.com/adafruit/io-client-ruby上的v1 api。有一个尚未合并的v2拉取请求,但该库使用法拉第而不是httparty。