A credential class that retrieves tokens from the cache only, without triggering interactive authentication flows. This is useful for non-interactive sessions where you want to use previously cached tokens from DeviceCode or AuthCode credentials.
Details
This credential attempts to retrieve cached tokens from a chain of interactive credentials (AuthCode and DeviceCode by default). It will not prompt for new authentication - it only returns tokens that are already cached.
This is particularly useful for:
Non-interactive R sessions (e.g., scheduled scripts, CI/CD)
Scenarios where you've previously authenticated interactively and want to reuse those cached tokens
Public fields
.scopeCharacter string specifying the authentication scope.
.tenant_idCharacter string specifying the tenant ID.
.client_idCharacter string specifying the client ID.
.chainList of credential classes to attempt for cached tokens.
Methods
Method new()
Create a new CachedTokenCredential object
Usage
CachedTokenCredential$new(
scope = NULL,
tenant_id = NULL,
client_id = NULL,
chain = cached_token_credential_chain()
)Arguments
scopeOptional character string specifying the authentication scope.
tenant_idOptional character string specifying the tenant ID for authentication.
client_idOptional character string specifying the client ID for authentication.
chainA list of credential classes to attempt for cached tokens. Defaults to AuthCodeCredential and DeviceCodeCredential.
Method get_token()
Get an access token from the cache
Returns
An httr2::oauth_token() object containing the access token
Examples
if (FALSE) { # \dontrun{
# Create credential with default settings
cred <- CachedTokenCredential$new(
scope = "https://graph.microsoft.com/.default",
tenant_id = "my-tenant-id"
)
# Get a cached token (will fail if no cached token exists)
token <- cred$get_token()
# Use with httr2 request
req <- httr2::request("https://graph.microsoft.com/v1.0/me")
req <- cred$req_auth(req)
} # }