Last active
September 18, 2024 13:51
-
-
Save mirko77/3f4a101cd4a77e2ae3e760d44d18d901 to your computer and use it in GitHub Desktop.
Fetch data from Epicollect5 API using R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(jsonlite) # if needing json format | |
cID<-"999" # client ID | |
secret<- "F00HaHa00G" # client secret | |
proj.slug<- "YourProjectSlug" # project slug | |
form.ref<- "YourFormRef" # form reference | |
branch.ref<- "YourFromRef+BranchExtension" # branch reference | |
res <- POST("https://five.epicollect.net/api/oauth/token", | |
body = list(grant_type = "client_credentials", | |
client_id = cID, | |
client_secret = secret)) | |
http_status(res) | |
token <- content(res)$access_token | |
# url.form<- paste("https://five.epicollect.net/api/export/entries/", proj.slug, "?map_index=0&form_ref=", form.ref, "&format=json", sep= "") ## if using json | |
url.form<- paste("https://five.epicollect.net/api/export/entries/", proj.slug, "?map_index=0&form_ref=", form.ref, "&format=csv&headers=true", sep= "") | |
res1<- GET(url.form, add_headers("Authorization" = paste("Bearer", token))) | |
http_status(res1) | |
# ct1<- fromJSON(rawToChar(content(res1))) ## if using json | |
ct1<- read.csv(res1$url) | |
str(ct1) | |
# url.branch<- paste("https://five.epicollect.net/api/export/entries/", proj.slug, "?map_index=0&branch_ref=", branch.ref, "&format=json&per_page=1000", sep= "") ## if using json; pushing max number of records from default 50 to 1000 | |
url.branch<- paste("https://five.epicollect.net/api/export/entries/", proj.slug, "?map_index=0&branch_ref=", branch.ref, "&format=csv&headers=true", sep= "") | |
res2<- GET(url.branch, add_headers("Authorization" = paste("Bearer", token))) | |
http_status(res2) | |
ct2<- read.csv(res2$url) | |
# ct2<- fromJSON(rawToChar(content(res2))) ## if using json | |
str(ct2) |
Great, I am glad the issue is solved!
That is interesting. I used exactly the code from above. The Jason one works fine. But the CSV one works fine until the ct1<- read.csv(res1$url).
@jimer666 I am facing the same issue you had, I am able to get the entries in the JSON format but the csv does not work. Were you able to resolve this issue?
Thank you
This line gives the error
ct1<- read.csv(res1$url)
Error in file(file, "rt") : cannot open the connection
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apologies for the delay- it was an error message. I would imagine it was happening because of too many calls when I was developing this as it disappeared once I left it all for a day or two and added time breaks between API calls. Thank you!