This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
bearer_token.R
54 lines (53 loc) · 2.08 KB
/
bearer_token.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#' Bearer token
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `bearer_token()` has been deprecated because it used a rather indirect
#' method of generating a bearer token. Instead, use [rtweet_app()], copying
#' in the bearer token directly from the
#' [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps).
#' See `vignette("auth", package = "rtweet")` for full details.
#' @keywords internal
#' @seealso [rtweet_bearer()], [`rtweet-deprecated`]
#' @export
bearer_token <- function(token = NULL) {
lifecycle::deprecate_stop("1.0.0", "bearer_token()", "rtweet_app()")
}
#' Invalidate bearer token
#'
#' Invalidate the bearer token automatically if you know the API key and API secret.
#'
#' @note Not tested!
#' @inheritParams rtweet_user
#' @param token Expert use only. Use this to invalidate a specific bearer token
#' created with [rtweet_app()]. If `NULL` the default authentication mechanism is invalidated.
#' @references <https://developer.twitter.com/en/docs/authentication/api-reference/invalidate_bearer_token>
#' <https://developer.twitter.com/en/docs/authentication/api-reference/invalidate_access_token>
#' @keywords internal
#' @export
invalidate_bearer <- function(api_key, api_secret, client = NULL, token = NULL) {
# See also rtweet_invalidate in auth.R
# To store the token at the right place: see ?httr2::oauth_cache_path
withr::local_envvar(HTTR2_OAUTH_CACHE = auth_path())
if (is.null(client)) {
client <- client_as(client)
api_key <- client["id"]
api_secret <- client["secret"]
} else {
stopifnot(is_string(api_key), is_string(api_secret))
}
if (is.null(client) && missing(api_key)) {
api_key <- ask_pass("API key")
}
if (is.null(client) && missing(api_secret)) {
api_key <- ask_pass("API secret")
}
token <- check_token_v2(token)
req1 <- httr2::request("https://api.twitter.com/oauth2/invalidate_token")
req2 <- httr2::req_url_query(req1, access_token = token)
req3 <- httr2::req_method(req2, "POST")
req4 <- httr2::req_auth_basic(req3, api_key, api_secret)
resp <- httr2::req_perform(req4)
resp
}