Skip to content

Commit

Permalink
New PR for (r-lib#460)
Browse files Browse the repository at this point in the history
* added config argument for oauth2.0 and init_oauth

* updatign so I can merge

* Fixing r-lib#411 (review) and example for write_stream.  Added to news.

* fixing odd rproj

* Splitting the PR for later.

* Updating news.

* updated back to roxygennote

* updated with styling

*  fixing indenting!!!!
  • Loading branch information
muschellij2 authored and hadley committed Jul 27, 2017
1 parent 4b74776 commit 20b26b1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 14 additions & 4 deletions R/oauth-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 6 additions & 3 deletions R/oauth-token.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions demo/oauth2-reddit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
6 changes: 5 additions & 1 deletion man/init_oauth2.0.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/oauth2.0_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 20b26b1

Please sign in to comment.