任务计划程序无法自动刷新陈旧的OAuth令牌

ss2ws0br  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在使用R脚本和软件包rdrop 2来自动上传一个不断更新的文件到Dropbox。代码在本地工作正常,并将在任务计划程序中按需运行4小时,但随后停止更新并“冻结”,因为任务计划程序指示任务“运行”了一段较长的时间。当我进入任务计划程序,手动结束,然后运行任务,它似乎修复自己,并罚款为另一个四个小时,然后同样的问题再次发生
我怀疑这与我用this code设置的可刷新令牌有关。有一段时间,我使用sink(见下文)将任何错误消息发送到error_log文件,当这种情况发生时,error_log文件将读取“自动刷新陈旧的OAuth令牌”。我怀疑sink函数是阻止代码继续的原因,因为当这个“错误”在Rstudio中发生时,代码继续执行并按预期执行上传/下载请求,所以我删除了sink,但问题仍然存在(作为参考,当任务运行完成时,error_log将读取类似于“File biostats.csv uploaded as /auto_test/biostats.csv successfully at 2023-06- 28 T17:36:03 Z”的内容)。
同样,我在任务调度程序中通过执行Rscript.exe并将Rscript作为参数传递(我将Rscript.exe的位置添加到系统环境PATH中)来运行它,并且它大部分时间都执行得很好,我只是觉得当它需要刷新过时的令牌时会挂起,我不知道如何修复它。
下面是Rscript:

library(rdrop2)
library(httr)
library(dplyr)

setwd("C:/Users/benke/Documents/R Main Directory/Data Upload Automation")

error_log <- file("C:/Users/benke/Documents/R Main Directory/Data Upload Automation/error_log.Rout", 
                  open = "wt")

sink(error_log, type = "message")

#Set up refreshable access token, can pass this to any 'drop' function in 'dtoken' argument
.dstate <- new.env(parent = emptyenv())

drop_auth_RT <- function (new_user = FALSE, key = "placeholder", secret = "placeholder", 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()

#Upload updated copy of file
drop_upload(file = "biostats.csv", path = "/auto_test",
            mode = "overwrite",
            dtoken = refreshable_token)

字符串

6ie5vjzr

6ie5vjzr1#

同样的情况也发生在我身上,我使用相同的代码片段来获取可刷新令牌。我得到的错误是:

Auto-refreshing stale OAuth token.
<http_408 in drop_upload(file = ...

字符串
在我的例子中,它总是发生在特定的drop_upload调用期间。所以,我的技巧是tryCatch这个调用,在catch中,再次调用drop_auth()然后drop_upload()。我甚至不确定drop_auth()调用是否必要。无论如何,对我来说,这每次都很有效。

tryCatch({
   drop_upload(file = local_path, path = db_path, mode = mode, dtoken = token)
   },
   error = function(e) {
      print(e)
      print(paste("Could not upload file:", file, "Trying again after re-auth."))
      drop_auth(rdstoken = "token.rds")  //same refreshable token as before
      drop_upload(file = local_path, path = db_path, mode = mode, dtoken = token)
   })

相关问题