Created
June 15, 2021 05:12
-
-
Save pdparker/c586c00f6b7fce1b8d939b1a285fabdd to your computer and use it in GitHub Desktop.
Access to sciwheel api in R
This file contains hidden or 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
# 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