0

I am new-ish to Python and am trying to use an API in my Jupyter notebook, which requires a token. The API is from NOAA.

I put my request in the format given on NOAA's website. Everything below is literally what I put in my code with the exception of the token itself, replaced as MYLONGTOKEN:

curl -H "token:<MYLONGTOKEN>" "https://www.ncdc.noaa.gov/cdo-web/api/v2/stations"
$.ajax({ url:<url>, data:{<data>}, headers:{ token:<token> } })

I get back a syntax error pointing at the " after MYLONGTOKEN. I have tried taking it out of the brackets and quotes, adding spaces, etc but can't seem to get rid of the error. I'm also not clear if the < token > on the second line is supposed to be replaced with the actual token, but I haven't gotten past the error in the first line to find out.

I also tried following a different syntax offered on a previous post but that gave me a JSONDECODE error.

I'm using python3 and wondering if NOAA's code might be outdated.

Any help is appreciated! Thank you.

Kelly
  • 15
  • 4
  • 3
    I don't see any Python code. – Klaus D. Jun 22 '20 at 03:52
  • 1
    You say that everything is literally what you put in other than the token, but clearly you are meant to substitute actual values for each of the `<....>` items. – alani Jun 22 '20 at 03:55
  • Anyway I don't think you are meant to include the `.ajax` stuff, this is an alternative, not part of the curl command. – alani Jun 22 '20 at 04:04
  • In addition to to the previous comments: please add the error message. – Stefan Scheller Jun 22 '20 at 04:04
  • 1
    If you are trying to call from python, you will want to use `requests` library and use `headers` parameter wth dictionary containing `{'token': 'XxYyZz.....'}' or whatever. I'll add an answer in a moment with an actual example.... – alani Jun 22 '20 at 04:07

1 Answers1

1

Example Python code:

import requests

token = "setljksetaafjlkasetaljksetasetdt"  # not a real access key obviously

url = "https://www.ncdc.noaa.gov/cdo-web/api/v2/stations"

response = requests.get(url, headers={'token': token})

data = response.json()
results = data["results"]

print("num results {}".format(len(results)))

for result in results[:2]:  # show first two results
    print()
    for k, v in result.items():
        print("{} => {}".format(k, v))

Output:

num results 25

elevation => 139
elevationUnit => METERS
mindate => 1948-01-01
longitude => -85.2482
name => ABBEVILLE, AL US
datacoverage => 0.8813
id => COOP:010008
latitude => 31.5702
maxdate => 2014-01-01

elevation => 249.3
elevationUnit => METERS
mindate => 1938-01-01
longitude => -87.1814
name => ADDISON, AL US
datacoverage => 0.5059
id => COOP:010063
latitude => 34.2553
maxdate => 2015-11-01

Note: presumably the different endpoints return different structures inside the JSON. The above is an example based on what I found using the stations URL that you quoted in the question. Look inside the data returned from response.json() to see what you get, as I don't know whether the results from all the different endpoints necessarily contain an element called 'results'.

The point of the curl ... thing in their documentation would be that if you were going to call from the Linux command line instead of in a Python script, you would do something like:

curl -H "token:setljksetaafjlkasetaljksetasetdt" "https://www.ncdc.noaa.gov/cdo-web/api/v2/stations"

so what this is saying (because the -H option in curl is for adding headers) is that you need to add an http request header called token, which in the Python code above is implemented using the dictionary that is passed via the headers parameter to the requests.get call.

And separately, on the next line, they also give an example of access via an Ajax call.

alani
  • 12,573
  • 2
  • 13
  • 23
  • Thanks so much! I h ad no idea what ajax or curl were for, so I appreciate the explanation. – Kelly Jun 24 '20 at 01:30