×

Notice

The forum is in read only mode.

REST API Authentication?

1 year 5 months ago #2422 by DJDevon3
Most of the documentation is geared toward curl but I'm using a microcontroller that only has REST GET/POST.  My microcontroller has no way of logging into a website, it has no browser.  I'm using an Adafruit Feather ESP32-S3 with Circuit Python and adafruit_requests library. I've written many API examples for Adafruit and currently working on an OpenSky-Network API. 

If I visit on my PC (without logging in) I can see I have "Too many requests". If I login to the site and visit the same url I can see I have about 3900 left. So while I'm good on PC there's no way to authenticate on my microcontroller using REST GET?

Also when I hit the limit the JSON page disappears in favor of an HTML page with "Too many requests".  Please instead of removing the JSON can you exchange for a JSON error message instead?  By removing JSON completely it crashes any JSON 's not really cool.

The error messages in the attached image of my own making after playing with OSN for a few hours and finding out there is a limit and it doesn't act nicely when you exceed it.

 
Attachments:
1 year 5 months ago #2423 by strohmeier
Replied by strohmeier on topic REST API Authentication?
Hi,

Thanks for your message. Let's be realistic, that is a very fringe use case and we don't have resources to look into/let alone deal with that. So you will need to work around it on your side. Sorry I don't have better news for you.
1 year 5 months ago - 1 year 5 months ago #2429 by DJDevon3
Replied by DJDevon3 on topic REST API Authentication?
I figured it out mostly from reading forum posts here. Would be lovely if you could add an example to the REST API documentation..

I've submitted 3 examples to Adafruit's adafruit_requests library examples for API integrations for OpenSky-Network.

Base64 Encoding the username:password string into the header is definitely required.
In this example "username:password" base64 encodes to IFVTRVJOQU1FOlBBU1NXT1JEIA==\n
Example API HEADER: {'Authorization': 'Basic IFVTRVJOQU1FOlBBU1NXT1JEIA==\\n'}
Example API GET URL: OpenSky base URL/api/states/all?icao24=7c6b2d

osnu = "username"
osnp = "password"
transponder = "7c6b2d"
base_url = "insert opensky base url here"
osn_cred = str(osnu) + ":" + str(osnp)
bytes_to_encode = b" " + str(osn_cred) + " "
base64_string = (bytes_to_encode)
basepw = repr(base64_string)[2:-1]

osn_header = {"Authorization": "Basic " + str(basepw)}
OPENSKY_SOURCE = " (base_url)" + "icao24=" + transponder
opensky_response = requests(url=OPENSKY_SOURCE, headers=osn_header).json()

dump_object = (opensky_response)
print("JSON Dump: ", dump_object)

osn_flight = opensky_response["time"]
print("Current Unix Time: ", osn_flight)

This is Adafruit Circuit Python code so likely won't be a direct translation for python users. You can probably infer a lot if you're a regular python user.  Now have OpenSky working on an ESP32-S3 using a simple GET request. :)
Attachments:
The following user(s) said Thank You: strohmeier
1 year 5 months ago #2430 by DJDevon3
Replied by DJDevon3 on topic REST API Authentication?
There's no edit or preview otherwise I would have tried to ensure it was formatted better for the forum. Seems the URL's were removed, and in the code. Can't edit so it will live like that. Should be enough for anyone in the future looking for REST API info. I think all the important info is in there.
1 year 5 months ago #2431 by DJDevon3
Replied by DJDevon3 on topic REST API Authentication?
Probably worth noting that the world of embedded systems on microcontrollers is growing. Kind of depends on what your idea of fringe is. ;)

The Adafruit discord server has 3K-4K active members online at any time, that's a broad audience worth catering to especially considering the technical sophistication of their average user.
Attachments:
Powered by Kunena Forum