diff --git a/NAMESPACE b/NAMESPACE index f02d705e..8a04df71 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export("%>%") export(curl_help) export(curl_translate) export(default_redirect_uri) +export(example_github_client) export(example_url) export(jwt_claim) export(jwt_encode_hmac) diff --git a/NEWS.md b/NEWS.md index d046830c..943b8ad2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # httr2 (development version) +* `req_oauth_device()` now takes a `auth_url` parameter making it usable + (#331, @taerwin). + +* OAuth errors containing a url now correctly display that URL + (instead of just uri). + * New `req_paginate()` and `paginate_req_perform()` to easily request paginated APIs (@mgirlich, #8). diff --git a/R/oauth-flow-auth-code.R b/R/oauth-flow-auth-code.R index 0a2adeed..7ea1302f 100644 --- a/R/oauth-flow-auth-code.R +++ b/R/oauth-flow-auth-code.R @@ -39,18 +39,16 @@ #' @returns A modified HTTP [request]. #' @inheritParams oauth_flow_auth_code #' @examples -#' client <- oauth_client( -#' id = "28acfec0674bb3da9f38", -#' secret = obfuscated(paste0( -#' "J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv", -#' "PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo" -#' )), -#' token_url = "https://github.com/login/oauth/access_token", -#' name = "hadley-oauth-test" -#' ) +#' req_auth_github <- function(req) { +#' req_oauth_auth_code( +#' req, +#' client = example_github_client(), +#' auth_url = "https://github.com/login/oauth/authorize" +#' ) +#' } #' #' request("https://api.github.com/user") %>% -#' req_oauth_auth_code(client, auth_url = "https://github.com/login/oauth/authorize") +#' req_auth_github() req_oauth_auth_code <- function(req, client, auth_url, cache_disk = FALSE, diff --git a/R/oauth-flow-device.R b/R/oauth-flow-device.R index dc452b73..048d94bb 100644 --- a/R/oauth-flow-device.R +++ b/R/oauth-flow-device.R @@ -13,11 +13,17 @@ #' @inheritParams req_oauth_auth_code #' @returns A modified HTTP [request]. #' @examples -#' client <- oauth_client("example", "https://example.com/get_token") -#' req <- request("https://example.com") +#' req_auth_github <- function(req) { +#' req_oauth_device( +#' req, +#' client = example_github_client(), +#' auth_url = "https://github.com/login/device/code" +#' ) +#' } #' -#' req %>% req_oauth_device(client) -req_oauth_device <- function(req, client, +#' request("https://api.github.com/user") %>% +#' req_auth_github() +req_oauth_device <- function(req, client, auth_url, cache_disk = FALSE, cache_key = NULL, scope = NULL, @@ -26,6 +32,7 @@ req_oauth_device <- function(req, client, params <- list( client = client, + auth_url = auth_url, scope = scope, auth_params = auth_params, token_params = token_params diff --git a/R/oauth-flow.R b/R/oauth-flow.R index fbf4a80e..2f341e3c 100644 --- a/R/oauth-flow.R +++ b/R/oauth-flow.R @@ -40,7 +40,7 @@ oauth_flow_abort <- function(error, c( "OAuth failure [{error}]", "*" = description, - i = if (!is.null(uri)) "Learn more at {.url uri}." + i = if (!is.null(uri)) "Learn more at {.url {uri}}." ), code = error, class = c(glue("httr2_oauth_{error}"), "httr2_oauth"), diff --git a/R/test.R b/R/test.R index 5cd68c5c..49a72c92 100644 --- a/R/test.R +++ b/R/test.R @@ -30,14 +30,18 @@ request_pagination_test <- function(parse_resp = NULL, } -#' URL to a local server that's useful for tests and examples +#' Code for examples #' -#' Requires the webfakes package to be installed. It has the following endpoints: +#' @description +#' `example_url()` runs a simple websever using the webfakes package with the +#' following endpoints: #' #' * all the ones from the [webfakes::httpbin_app()] #' * `/iris`: paginate through the iris dataset. It has the query parameters #' `page` and `limit` to control the pagination. #' +#' `example_github_client()` is an OAuth client for GitHub. +#' #' @keywords internal #' @export example_url <- function() { @@ -74,3 +78,18 @@ example_url <- function() { ) the$test_app$url() } + +#' @export +#' @rdname example_url +example_github_client <- function() { + # + oauth_client( + id = "28acfec0674bb3da9f38", + secret = obfuscated(paste0( + "J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv", + "PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo" + )), + token_url = "https://github.com/login/oauth/access_token", + name = "hadley-oauth-test" + ) +} diff --git a/man/example_url.Rd b/man/example_url.Rd index efc95466..7334cb87 100644 --- a/man/example_url.Rd +++ b/man/example_url.Rd @@ -2,18 +2,22 @@ % Please edit documentation in R/test.R \name{example_url} \alias{example_url} -\title{URL to a local server that's useful for tests and examples} +\alias{example_github_client} +\title{Code for examples} \usage{ example_url() + +example_github_client() } \description{ -Requires the webfakes package to be installed. It has the following endpoints: -} -\details{ +\code{example_url()} runs a simple websever using the webfakes package with the +following endpoints: \itemize{ \item all the ones from the \code{\link[webfakes:httpbin_app]{webfakes::httpbin_app()}} \item \verb{/iris}: paginate through the iris dataset. It has the query parameters \code{page} and \code{limit} to control the pagination. } + +\code{example_github_client()} is an OAuth client for GitHub. } \keyword{internal} diff --git a/man/req_oauth_auth_code.Rd b/man/req_oauth_auth_code.Rd index f2ce7cad..d4616915 100644 --- a/man/req_oauth_auth_code.Rd +++ b/man/req_oauth_auth_code.Rd @@ -106,16 +106,14 @@ create a new client than find your client secret. } \examples{ -client <- oauth_client( - id = "28acfec0674bb3da9f38", - secret = obfuscated(paste0( - "J9iiGmyelHltyxqrHXW41ZZPZamyUNxSX1_uKnv", - "PeinhhxET_7FfUs2X0LLKotXY2bpgOMoHRCo" - )), - token_url = "https://github.com/login/oauth/access_token", - name = "hadley-oauth-test" -) +req_auth_github <- function(req) { + req_oauth_auth_code( + req, + client = example_github_client(), + auth_url = "https://github.com/login/oauth/authorize" + ) +} request("https://api.github.com/user") \%>\% - req_oauth_auth_code(client, auth_url = "https://github.com/login/oauth/authorize") + req_auth_github() } diff --git a/man/req_oauth_device.Rd b/man/req_oauth_device.Rd index 59eadefe..32aeafda 100644 --- a/man/req_oauth_device.Rd +++ b/man/req_oauth_device.Rd @@ -7,6 +7,7 @@ req_oauth_device( req, client, + auth_url, cache_disk = FALSE, cache_key = NULL, scope = NULL, @@ -19,6 +20,9 @@ req_oauth_device( \item{client}{An \code{\link[=oauth_client]{oauth_client()}}.} +\item{auth_url}{Authorization url; you'll need to discover this by reading +the documentation.} + \item{cache_disk}{Should the access token be cached on disk? This reduces the number of times that you need to re-authenticate at the cost of storing access credentials on disk. Cached tokens are encrypted, @@ -47,8 +51,14 @@ the number of times the flow is performed. Learn more about the overall flow in \code{vignette("oauth")}. } \examples{ -client <- oauth_client("example", "https://example.com/get_token") -req <- request("https://example.com") +req_auth_github <- function(req) { + req_oauth_device( + req, + client = example_github_client(), + auth_url = "https://github.com/login/device/code" + ) +} -req \%>\% req_oauth_device(client) +request("https://api.github.com/user") \%>\% + req_auth_github() } diff --git a/tests/testthat/_snaps/oauth-flow-auth-code.md b/tests/testthat/_snaps/oauth-flow-auth-code.md index 954cf11e..241164e6 100644 --- a/tests/testthat/_snaps/oauth-flow-auth-code.md +++ b/tests/testthat/_snaps/oauth-flow-auth-code.md @@ -46,7 +46,7 @@ Error in `oauth_flow_auth_code_parse()`: ! OAuth failure [123] * A bad error - i Learn more at . + i Learn more at . Code oauth_flow_auth_code_parse(query3, "abc") Condition