Skip to content

Instantly share code, notes, and snippets.

@pdparker
Created June 15, 2021 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdparker/c586c00f6b7fce1b8d939b1a285fabdd to your computer and use it in GitHub Desktop.
Save pdparker/c586c00f6b7fce1b8d939b1a285fabdd to your computer and use it in GitHub Desktop.
Access to sciwheel api in R
# libraries you will need
library(httr)
library(jsonlite)
# You will need a token from sciwheel API
token = '<<your API token goes here>>'
# Get Project Ids for use in sciwheel_bib
sciwheel_projects <- function(token){
project = 'https://sciwheel.com/extapi/work/projects'
get_meta <- GET(project, add_headers("Authorization"= paste0("Bearer ", token)))
status <- httr::status_code(get_meta)
if(status == 401){
stop("User authorization error (e.g., invalid authorization token), or client has broken the rate limit")
}
if(status == 403){
stop("Insufficient privileges for the request")
}
if(status == 500){
stop("Server-related issue. Please contact Sciwheel if the error persists")
}
meta_content <- content(get_meta,"text")
meta_out <- fromJSON(meta_content, flatten = TRUE)
meta_out
}
# Get a .bib of a project and save to file
sciwheel_bib <- function(projectId, file_name ){
base = 'https://sciwheel.com/extapi/work/references/export?'
get_bib <- GET(paste0(base, "projectId=",projectId), add_headers("Authorization"= paste0("Bearer ", token)))
status <- httr::status_code(get_bib)
if(status == 401){
stop("User authorization error (e.g., invalid authorization token), or client has broken the rate limit")
}
if(status == 403){
stop("Insufficient privileges for the request")
}
if(status == 500){
stop("Server-related issue. Please contact Sciwheel if the error persists")
}
bib_out <- content(get_bib, "text")
cat(bib_out,file = file_name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment