User-Agent request header = string IDing the “user agent”
User-Agent headerhttr2::req_user_agent(req, string = NULL)
.httr2_ua <- function() {
# Recreate the default httr2 string.
versions <- c(
httr2 = as.character(utils::packageVersion("httr2")),
`r-curl` = as.character(utils::packageVersion("curl")),
libcurl = curl::curl_version()$version
)
paste0(names(versions), "/", versions, collapse = " ")
}
.req_ua <- function(req) {
httr2_string <- .httr2_ua()
me <- "Jon Harmon"
url <- "https://wapir.io"
email <- "jonthegeek+useragent@gmail.com"
string <- glue::glue(
"{httr2_string} ({me}; {url}; mailto:{email})"
)
httr2::req_user_agent(req, string = string)
}🔴 HTTP Basic: username + password sent with request
🟠 API Key: password-like thing sent with request
🟡 Bearer Token: shorter-lived, limited key
🟢 OAuth: multistep process to generate a bearer token
If you have the APID, use it!
components$securitySchemes = ways to authsecurity (top level) = default schemesusethis::git_vaccinate()usethis::use_git_ignore(".Renviron")install.packages("keyring")keyring::key_set(service)
keyring::key_set("FEC_API_KEY")keyring::key_set_with_value(service, password = NULL)keyring::key_get(service)Sys.setenv(FEC_API_KEY = keyring::key_get("FEC_API_KEY"))in: query
httr2::req_url_query(.req, ...)in: header
httr2::req_headers(.req, ..., .redact = NULL).redact = character vector of headers to hide in printin: cookie
httr2::req_headers(.req, Cookie = "name=val1; name2=val2", .redact = "Cookie"){nectar} 📦 translates APID to {httr2}
| term(s) | meaning |
|---|---|
| user | the person who you’re acting as |
| application, app | your R code |
| client, oauth application | you create this at oauth host |
| oauth host | the API |
| term(s) | meaning |
|---|---|
| scope | string(s) describing specific capabilities |
| authorization code | very temporary key |
| oauth token, token | the real key, often with extra info |
| term(s) | meaning |
|---|---|
| authorization url | where to send initial request |
| redirect url(s) | where host can send auth codes |
| token url | where app can exchange auth codes |
Construct one client object for your code
playlists <- request("https://youtube.googleapis.com/youtube/v3") |>
req_url_path_append("playlists") |>
req_url_query(part = "snippet", mine = TRUE, maxResults = 50) |>
req_oauth_auth_code(
client = yt_client,
auth_url = "https://accounts.google.com/o/oauth2/v2/auth",
scope = "https://www.googleapis.com/auth/youtube",
redirect_uri = "http://127.0.0.1:8888"
) |>
req_perform()httr2::req_oauth_bearer_jwt() if you have JSON web token (service account)httr2::req_oauth_refresh() if you have a refresh token
httr2::oauth_flow_auth_code() once to get refreshThis will feel hacky because it is hacky.
pathhttr2::req_cookie_preserve(req, path)DSLC.io/wapir | Jon Harmon | wapir.io