Rya*_*tew 1 r dropbox-api rdrop2
我正在尝试创建一个闪亮的应用程序,它使用包 rdrop2 链接到我的保管箱。
我已成功部署该应用程序,它按计划运行了大约 4 个小时。但是,我需要持久的离线访问。Dropbox 帮助页面显示我需要“刷新令牌”。
目前,为了获取我正在使用的令牌:
library(rdrop2)
token <- drop_auth() # gets credentials
saveRDS(token, "droptoken.rds") # saves credentials
token<-readRDS("droptoken.rds") # read in credentials
drop_auth(new_user = FALSE,
cache = TRUE,
rdstoken = "droptoken.rds")
ui <- # some UI code
server <- function(input, output,session) {
# some server code
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
这将创建一个带有“sl”的令牌。访问令牌(短期),到期时间为 14400,即 4 小时。4 小时后,该应用程序由于“未经授权的 HTTP 401”错误而不再运行。
任何人都可以为我提供有关如何调整此代码以获取刷新令牌的帮助吗?
注意:dropbox 不再允许无期限的令牌(截至 2021 年 9 月),因此我需要沿着刷新令牌路线进行。
我不知道您是否仍然需要帮助,但我最近的任务是在实习计划中解决这个问题,并设法找到解决方案。
目前,rdrop2 的drop_auth()函数将生成具有以下凭据的令牌:<credentials> access_token, token_type, expires_in, uid, account_id。由于访问令牌现在仅持续 4 小时,因此我们需要刷新令牌作为凭证的一部分,以便我们可以在每次过期时获得新的访问令牌。
要生成带有刷新令牌的令牌对象,我们需要修改函数内的授权端点drop_auth()。这看起来像这样:
library(rdrop2)
.dstate <- new.env(parent = emptyenv())
drop_auth_RT <- function (new_user = FALSE, key = "mmhfsybffdom42w", secret = "l8zeqqqgm1ne5z0", cache = TRUE, rdstoken = NA)
{
if (new_user == FALSE & !is.na(rdstoken)) {
if (file.exists(rdstoken)) {
.dstate$token <- readRDS(rdstoken)
}
else {
stop("token file not found")
}
}
else {
if (new_user && file.exists(".httr-oauth")) {
message("Removing old credentials...")
file.remove(".httr-oauth")
}
dropbox <- httr::oauth_endpoint(authorize = "https://www.dropbox.com/oauth2/authorize?token_access_type=offline",
access = "https://api.dropbox.com/oauth2/token")
# added "?token_access_type=offline" to the "authorize" parameter so that it can return an access token as well as a refresh token
dropbox_app <- httr::oauth_app("dropbox", key, secret)
dropbox_token <- httr::oauth2.0_token(dropbox, dropbox_app,
cache = cache)
if (!inherits(dropbox_token, "Token2.0")) {
stop("something went wrong, try again")
}
.dstate$token <- dropbox_token
}
}
refreshable_token <- drop_auth_RT()
Run Code Online (Sandbox Code Playgroud)
这将生成具有以下凭据的令牌:<credentials> access_token, token_type, expires_in, refresh_token, uid, account_id。新令牌现在将具有刷新令牌,HTTP API 可使用该刷新令牌自动刷新访问令牌。
调用 rdrop2 函数时指定新令牌非常重要,否则它将无法工作。例子:
> drop_dir()
Error in drop_list_folder(path, recursive, include_media_info, include_deleted, :
Unauthorized (HTTP 401).
> drop_dir(dtoken = refreshable_token)
# A tibble: 4 x 11
.tag name path_lower path_display id client_modified server_modified rev size
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <int>
1 folder Screensh~ /screensh~ /Screenshots id:_~ NA NA NA NA
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!