View source: R/methods__client_bearer_req.R
| resource_req | R Documentation |
This is a helper for calling downstream APIs with an access token. It creates an
httr2::request() for the given URL, attaches the right authorization header
for the token type, and applies shinyOAuth's standard HTTP defaults.
Use perform_resource_req() when you want shinyOAuth to also perform the request
and handle DPoP nonce challenges for you (which httr2::req_perform()
would not do on its own).
resource_req(
token,
url,
method = "GET",
headers = NULL,
query = NULL,
follow_redirect = FALSE,
check_url = TRUE,
oauth_client = NULL,
token_type = NULL,
dpop_nonce = NULL
)
token |
Either an OAuthToken object or a raw access token string. |
url |
The absolute URL to call. |
method |
Optional HTTP method (character). Defaults to "GET". When
the effective token type is |
headers |
Optional named list or named character vector of extra
headers to set on the request. Header names are case-insensitive.
Any user-supplied |
query |
Optional named list of query parameters to append to the URL. |
follow_redirect |
Logical. If |
check_url |
Logical. If |
oauth_client |
Optional OAuthClient. Required when the effective
token type is |
token_type |
Optional override for the access token type when |
dpop_nonce |
Optional DPoP nonce to embed in the proof for this
request. This is primarily useful after a resource server challenges with
|
An httr2 request object, ready to be performed with
httr2::req_perform(). Callers may still add headers or query
parameters, but when the effective token type is DPoP they must not
change the request method or base URL after calling
resource_req() because the proof is already bound to those values.
DPoP proofs bind the current HTTP method and target URI (without query or
fragment). Adding query parameters after resource_req() is fine, but
changing the method, scheme, host, or path invalidates the proof.
# Make request using OAuthToken object
# (code is not run because it requires a real token from user interaction)
if (interactive()) {
# Get an OAuthToken
# (typically provided as reactive return value by `oauth_module_server()`)
token <- OAuthToken()
# Recommended for most callers: build + perform in one step.
response <- perform_resource_req(
token,
"https://api.example.com/resource",
query = list(limit = 5)
)
# Build only when you need to inspect the request yourself.
request <- resource_req(
token,
"https://api.example.com/resource",
query = list(limit = 5)
)
httr2::req_dry_run(request)
# Or start from your own httr2 request and still let shinyOAuth perform it
# so DPoP nonce retries remain available.
custom_request <- httr2::request("https://api.example.com/resource") |>
httr2::req_headers(Accept = "application/json") |>
httr2::req_url_query(limit = 5)
response <- perform_resource_req(token, custom_request)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.