AstronomyAPI / Samples

Examples for Astronomy API
https://astronomyapi.com
MIT License
30 stars 3 forks source link

API access from non-Web apps? #69

Closed Chuck1000gmx closed 3 months ago

Chuck1000gmx commented 5 months ago

Is it possible to access the API from non-Web hosted methods... using Excel VBA, VBScript, Powershell, or the like? I have a large list of items that I'm trying to query JSON data from the API, one item at a time. Using any of the methods I get an "ERROR_INTERNET_CONNECTION_RESET" (Error 12301).

I have little experience with web development.

alinselicean commented 4 months ago

Hi.

I am having a similar problem (probably). I am trying to call the API from an Access DB using VBA. All seem to be correct, but I am getting back the UNAUTHORIZED response.

Is it because the call is not initiated from a browser? I am trying to build a planner for my personal use, so nothing web based.

Thanks, Alin

Chuck1000gmx commented 4 months ago

No, I don't think the problem is because we're calling it from outside a browser. I can successfully call API's from other websites using VBA, VBS, and PS. I think it might have something to do with how the HTTP Request Header is formed. Browsers seem to add some extra tags to your request URL. The Astronomy API must be looking for some other info in the header. You can also add headers yourself in VBA using the MSXML2.XMLHTTP.setRequestHeader(header,value) method... https://codingislove.com/http-requests-excel-vba/

eg. .setRequestHeader "Content-Type", "text/json"

Instead, I installed Python for Windows. You can call scripts from a command prompt just like VBSscripts. I use a Python script to make the API call, save the data to a CSV file, and then read it into Excel. Its not a fully automated process but the few extra steps get around the problem.

I had zero experience with Python before this but it was fairly easy to hobble a script together with knowledge of VBA and VBS. I didn't even realize that you could install and run Python locally, but am glad it does.

Let me know if this interests you and I'll post more info on how to mimic what I've done.

astroanu commented 4 months ago

Hi, sorry for the late reply.

It could be you're sending some extra headers is causing this? could you send me the app id you're trying with? and the date you tried (a very recent -today, request would be good)

Chuck1000gmx commented 4 months ago

I don't think Excel's VBA or VBS or Powershell are sending any extra headers. Their syntax is quite similar and they appear to only send the URL that I use to make the API request... like when you enter it into a browser. When I do try to send extra headers, certain headers return different errors, but most extra headers return the same UNAUTHORIZED response.

No worries. I managed to get a local install of Python to work. Not being familiar with Python I thought it only existed from hosted web servers but a local Windows install works well for my needs.