diff --git a/NEWS.md b/NEWS.md index 3864f54b..06f6749f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # httr 1.2.1.9000 + +* Fix reddit demo where `user_agent` was being overloaded. Now can pass + `user_agent` using `config_init` to `init_oauth2.0` and `Token2.0$new()` + (@muschellij2 @hadley #363). + * `RETRY()` gains a new parameter `terminate_on` that gives caller greater control over which status codes make it stop retrying. It also retries if an error occurs during the request (@asieira, #404) diff --git a/R/oauth-init.R b/R/oauth-init.R index 2478d063..56975912 100644 --- a/R/oauth-init.R +++ b/R/oauth-init.R @@ -62,12 +62,14 @@ init_oauth1.0 <- function(endpoint, app, permission = NULL, #' retrieve the token. Some authorization servers require this. #' If \code{FALSE}, the default, retrieve the token by including the #' app key and secret in the request body. +#' @param config_init Additional configuration settings sent to +#' \code{\link{POST}}, e.g. \code{\link{user_agent}}. #' @export #' @keywords internal init_oauth2.0 <- function(endpoint, app, scope = NULL, user_params = NULL, type = NULL, use_oob = getOption("httr_oob_default"), is_interactive = interactive(), - use_basic_auth = FALSE) { + use_basic_auth = FALSE, config_init = list()) { scope <- check_scope(scope) use_oob <- check_oob(use_oob) @@ -108,11 +110,19 @@ init_oauth2.0 <- function(endpoint, app, scope = NULL, user_params = NULL, } if (isTRUE(use_basic_auth)) { - req <- POST(endpoint$access, encode = "form", body = req_params, - authenticate(app$key, app$secret, type = "basic")) + req <- POST(endpoint$access, + encode = "form", + body = req_params, + authenticate(app$key, app$secret, type = "basic"), + config = config_init + ) } else { req_params$client_secret <- app$secret - req <- POST(endpoint$access, encode = "form", body = req_params) + req <- POST(endpoint$access, + encode = "form", + body = req_params, + config = config_init + ) } stop_for_status(req, task = "get an access token") diff --git a/R/oauth-token.r b/R/oauth-token.r index f7a6b360..55c170fb 100644 --- a/R/oauth-token.r +++ b/R/oauth-token.r @@ -197,10 +197,12 @@ oauth2.0_token <- function(endpoint, app, scope = NULL, user_params = NULL, type = NULL, use_oob = getOption("httr_oob_default"), as_header = TRUE, use_basic_auth = FALSE, - cache = getOption("httr_oauth_cache")) { + cache = getOption("httr_oauth_cache"), + config_init = list()) { params <- list(scope = scope, user_params = user_params, type = type, use_oob = use_oob, as_header = as_header, - use_basic_auth = use_basic_auth) + use_basic_auth = use_basic_auth, + config_init = config_init) Token2.0$new(app = app, endpoint = endpoint, params = params, cache_path = cache) @@ -213,7 +215,8 @@ Token2.0 <- R6::R6Class("Token2.0", inherit = Token, list( self$credentials <- init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, user_params = self$params$user_params, type = self$params$type, use_oob = self$params$use_oob, - use_basic_auth = self$params$use_basic_auth) + use_basic_auth = self$params$use_basic_auth, + config_init = self$params$config_init) }, can_refresh = function() { !is.null(self$credentials$refresh_token) diff --git a/demo/oauth2-reddit.R b/demo/oauth2-reddit.R index 1c6cc8e9..249c8437 100644 --- a/demo/oauth2-reddit.R +++ b/demo/oauth2-reddit.R @@ -15,3 +15,12 @@ token <- oauth2.0_token(reddit, app, scope = c("read", "modposts"), use_basic_auth = TRUE ) + +# 3b. If get 429 too many requests, the default user_agent is overloaded. +# If you have an application on Reddit then you can pass that using: +token <- oauth2.0_token( + reddit, app, + scope = c("read", "modposts"), + use_basic_auth = TRUE, + config_init = user_agent("YOUR_USER_AGENT") +) diff --git a/man/init_oauth2.0.Rd b/man/init_oauth2.0.Rd index d1cd0805..ed54cd40 100644 --- a/man/init_oauth2.0.Rd +++ b/man/init_oauth2.0.Rd @@ -6,7 +6,8 @@ \usage{ init_oauth2.0(endpoint, app, scope = NULL, user_params = NULL, type = NULL, use_oob = getOption("httr_oob_default"), - is_interactive = interactive(), use_basic_auth = FALSE) + is_interactive = interactive(), use_basic_auth = FALSE, + config_init = list()) } \arguments{ \item{endpoint}{An OAuth endpoint, created by \code{\link{oauth_endpoint}}} @@ -33,6 +34,9 @@ or \code{TRUE} if \code{httpuv} is not installed.} retrieve the token. Some authorization servers require this. If \code{FALSE}, the default, retrieve the token by including the app key and secret in the request body.} + +\item{config_init}{Additional configuration settings sent to +\code{\link{POST}}, e.g. \code{\link{user_agent}}.} } \description{ See demos for use. diff --git a/man/oauth2.0_token.Rd b/man/oauth2.0_token.Rd index 2a8ff145..d2b06dd6 100644 --- a/man/oauth2.0_token.Rd +++ b/man/oauth2.0_token.Rd @@ -6,7 +6,8 @@ \usage{ oauth2.0_token(endpoint, app, scope = NULL, user_params = NULL, type = NULL, use_oob = getOption("httr_oob_default"), as_header = TRUE, - use_basic_auth = FALSE, cache = getOption("httr_oauth_cache")) + use_basic_auth = FALSE, cache = getOption("httr_oauth_cache"), + config_init = list()) } \arguments{ \item{endpoint}{An OAuth endpoint, created by \code{\link{oauth_endpoint}}} @@ -41,6 +42,9 @@ app key and secret in the request body.} using the default cache file \code{.httr-oauth}, \code{FALSE} means don't cache, and \code{NA} means to guess using some sensible heuristics. A string mean use the specified path as the cache file.} + +\item{config_init}{Additional configuration settings sent to +\code{\link{POST}}, e.g. \code{\link{user_agent}}.} } \value{ A \code{Token2.0} reference class (RC) object.