https://usda.library.cornell.edu/apidoc/index.html The web site has provided the API doc. Can somebody tell me the required steps to fetch the data either in R or Python?
Here's how you can do it in R.
First, create variables containing the email address and password you created when you signed up:
my_email <- "Niket_Chauhan@somewhere.com"
my_password <- "Passw0rd!"
You are going to need to send these inside a json string as a POST request to get an authorization code to use the service. We'll create the json now:
my_json <- paste0('{"auth": {"email":"', my_email,'","password":"', my_password, '"}}')
Now we'll request an authorization token using httr::POST
:
library(httr)
auth <- POST("https://usda.library.cornell.edu/user_token",
body = my_json, encode = "raw",
add_headers(`Content-Type` = "application/json"))
If everything went well, auth
now contains our authorization token. We will use this token inside an 'Authorization' header for every subsequent request to the server:
token <- add_headers("Authorization" = paste("Bearer", content(auth, "parsed")$jwt))
So now we can just send http requests to the API using GET
and POST
. For most of these we can just use the url we want, and ensure we have included our header.
res <- GET("https://usda.library.cornell.edu/api/v1/agency/findAll", token)
Here, res
is an http response object, so we'll extract its contents as text to get our json string:
cat(content(res, "text"))
#> [{"id":"waob_agency","title":["World Agricultural Outlook Board"],
#> "acronym":["WAOB"],"contact_email":["oce@oce.usda.gov"],"contact_phone":
#> ["202-720-5447"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.usda.gov/oce/commodity/"]},
#> {"id":"ers_agency","title":["Economic Research Service"],"acronym":
#> ["ERS"],"contact_email":["ers@ers.usda.gov"],"contact_phone":
#> ["202-694-5139"],"location_city":["Washington"],"location_state":
#> ["DC"],"homepage_url":["http://www.ers.usda.gov/"]},
#> {"id":"nass_agency","title":["National Agricultural Statistics Service"],
#> "acronym":["NASS"],"contact_email":["nass@nass.usda.gov"],
#> "contact_phone":["800-727-9540"],"location_city":["Washington"],
#> "location_state":["DC"],"homepage_url":["http://www.nass.usda.gov/"]},
#> {"id":"fas_agency","title":["Foreign Agricultural Service"],"acronym":
#> ["FAS"],"contact_email":["fas@fas.usda.gov"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.fas.usda.gov/"]},{"id":"ams_agency","title":
#> ["Agricultural Marketing Service"],"acronym":["AMS"],"contact_email":
#> ["ams@ams.usda.gov"],"contact_phone":["202-720-8998"],"location_city":
#> ["Washington"],"location_state":["DC"],"homepage_url":
#> ["http://www.ams.usda.gov/"]}]