rDrop dropbox api non-expiring tokens/seamless token issues

我只是一个虾纸丫 提交于 2019-12-22 10:28:41

问题


I am using the rDrop package that is available from https://github.com/karthikram/rDrop, and after a bit of tweaking (as all the functions don't quite work as you would always expect them to) I have got it to work finally in the way I would like, but it still requires authorisation verification to allow the use of the app, once you get the token each time, as I think that tokens expire over time...(if this is not the case and I can hard code in my token please tell me as that would be a good solution too...)

Basically I wanted a near seamless way of downloading csv files from my dropbox folders from the commandline in R in one line of code so that I dont need to click on the allow button after the token request.

Is this possible?

Here is the code I used to wrap up a dropbox csv download.

db.csv.download <- function(dropbox.path, ...){

cKey <- getOption('DropboxKey')
cSecret <- getOption('DropboxSecret')
reqURL <- "https://api.dropbox.com/1/oauth/request_token"
authURL <- "https://www.dropbox.com/1/oauth/authorize"
accessURL <- "https://api.dropbox.com/1/oauth/access_token/"

require(devtools)
install_github("ROAuth", "ropensci")
install_github("rDrop", "karthikram")
require(rDrop)
dropbox_oa <- oauth(cKey, cSecret, reqURL, authURL, accessURL, obj = new("DropboxCredentials"))
cred <- handshake(dropbox_oa, post = TRUE)
raw.data <- dropbox_get(cred,dropbox.path)
data <- read.csv(textConnection(raw.data), ...)
data
}

Oh and if its not obvious I have put my dropbox key and secret in my .Rprofile file, which is what the getOption part is referring to.

Thanks in advance for any help that is provided. (For bonus points...if anybody knows how to get rid of all the loading messages even for the install that would be great...)


回答1:


library(rDrop) 
# my keys are in my .rprofile, otherwise specifiy inline
db_token <- dropbox_auth()
# Hit ok to authorize once through the browser and hit enter back at the R prompt.
save(db_token, file="my_dropbox_token.rdata")

Dropbox token are non-expiring and can be revoked anytime from the Dropbox web panel.

For future use:

library(rDrop)
load('~/Desktop/my_dropbox_token.rdata')
df <- data.frame(x=1:10, y=rnorm(10))
> df
    x          y
1   1 -0.6135835
2   2  0.3624928
3   3  0.5138807
4   4 -0.2824156
5   5  0.9230591
6   6  0.6759700
7   7 -1.9744624
8   8 -1.2061920
9   9  0.9481213
10 10 -0.5997218
dropbox_save(db_token, list(df), file="foo", ext=".rda")
rm(df)
df2 <- db.read.csv(db_token, file='foo.rda')
> df2
    x          y
1   1 -0.6135835
2   2  0.3624928
3   3  0.5138807
4   4 -0.2824156
5   5  0.9230591
6   6  0.6759700
7   7 -1.9744624
8   8 -1.2061920
9   9  0.9481213
10 10 -0.5997218

If you have additional problems, please file an issue.



来源:https://stackoverflow.com/questions/12402601/rdrop-dropbox-api-non-expiring-tokens-seamless-token-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!