-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.R
54 lines (46 loc) · 1.16 KB
/
sample.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
library(httr)
library(jsonlite)
#' OAUTH2 Token
#'
#' Get the OAUTH2 token with httr.
#'
icd_token <- function(client_id = NULL, client_secret = NULL) {
if (is.null(client_id)) client_id <- readline("Enter client id: ")
if (is.null(client_secret)) client_secret <- readline("Enter client secret: ")
httr::init_oauth2.0(
endpoint = httr::oauth_endpoint(
authorize = NULL,
access = "https://icdaccessmanagement.who.int/connect/token"
),
app = httr::oauth_app(
appname = "icd",
key = client_id,
secret = client_secret
),
scope = "icdapi_access",
client_credentials = TRUE
)
}
#' Token as a Header
#'
#' Prepare the header for the GET request with httr.
#'
token_as_header <- function(token) {
httr::add_headers(
Authorization = paste(token$token_type, token$access_token),
Accept = "application/ld+json",
"Accept-Language" = "en",
"API-Version" = "v2"
)
}
# Get a token
token <- icd_token()
# Send the request
result <- GET(
url = "https://id.who.int/icd/entity",
token_as_header(token)
)
# Print result
result
# Print result content
fromJSON(content(result, as = "text", encoding = "UTF-8"))