REF : NYTimes Book API REF: https://developer.nytimes.com/docs/books-product/1/overview https://httr2.r-lib.org/articles/httr2.html
knitr::opts_chunk$set(echo = TRUE, comment = " ##", error = TRUE, collapse = TRUE) library(httr2) load_all()
nyt_key <- Sys.getenv("nyt_key") nyt_secret <- Sys.getenv("nyt_secret") resp <- request("https://api.nytimes.com/svc/books/v3") %>% req_url_path_append("/reviews.json") %>% req_url_query(`api-key` = nyt_key, isbn = 9780307476463) %>% req_perform() resp ## Examine body resp %>% resp_body_json() %>% str() ``` #### Suppose error, 401 unauthorized ```r resp <- request("https://api.nytimes.com/svc/books/v3") %>% req_url_path_append("/reviews.json") %>% req_url_query(`api-key` = "invalid", isbn = 9780307476463) %>% req_perform() last_request() last_response() last_response() |> resp_body_json() ## New way to crawl JSON last_response() %>% resp_body_json() %>% .$fault last_response() %>% resp_body_json() %>% .$fault %>% .$faultstring ## |> does not support ! last_response() |> resp_body_json() |> .$fault
## replace httr2:: function with wrapper. Makes default to NOT redact. req_dry_run <- function(..., redact_headers=F) { httr2::req_dry_run(..., redact_headers=redact_headers) } ## SET/GET Sys.setenv(FAKE="joke") Sys.getenv("FAKE") system("echo hi") system2("v=FAKE")
writeLines(text = "FOO", con = "_secret_file") readLines("_secret_file")
## github token, for GIST only HTTR2_GIST <- Sys.getenv("HTTR2_GIST") HTTR2_GIST writeLines(text = paste0("HTTR2_GIST=", HTTR2_GIST), con= " _HTTR2_GIST") token <- HTTR2_GIST
## This is Hadley's client/ I can't recreate my own: what is URL?? client <- oauth_client( id = "28acfec0674bb3da9f38", secret = obfuscated("J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnvPeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo"), token_url = "https://github.com/login/oauth/access_token", name = "hadley-oauth-test" ) client ## github window pops up to authorizee token <- oauth_flow_auth_code(client, auth_url = "https://github.com/login/oauth/authorize") # has no scope! token
# First, he says do htis and it prints "jim" request("https://api.github.com/user") %>% req_auth_bearer_token(token$access_token) %>% req_perform() %>% resp_body_json() %>% .$name # # Then he says this is better, which prints stuff about me. request("https://api.github.com/user") %>% req_oauth_auth_code(client, auth_url = "https://github.com/login/oauth/authorize") %>% req_perform() %>% resp_body_json() ## find cached tokens? rappdirs::user_cache_dir("httr2") # [1] "/home/jim/.config/cache/httr2" dir(rappdirs::user_cache_dir("httr2"), recursive = TRUE) # character(0)
library(httr2) client <- oauth_client( id = "Iv1.73a8cd0fc360044f", secret = obfuscated("G8mAhZD5SVZKf0dEkrTA33xf6ImWrhIfTrRuZzb9JxH87ec8LglCilQGdbonyS1f6oI-ytqxOCk"), token_url = "https://github.com/login/oauth/access_token", name = "github_app" ) client token <- oauth_flow_device(client, auth_url = "https://github.com/login/device/code")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.