简体   繁体   中英

adding an api key for httparty in ruby on rails

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

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 "}

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.

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

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

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.

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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