2011-05-23 26 views
25

我爱RGoogleDocs并使用它很多。但是,我不喜欢随时输入我的密码。显然,我可以在R脚本中输入密码,而不必再次输入密码。但那不是可行的,因为这意味着我的密码将在我的硬盘上保持未加密。此外,我与同事分享我的脚本。如何防止在使用RGoogleDocs时暴露我的密码?

为了解决这个问题,我想出了这个。

if(exists("ps")){ 
    print("got password, keep going") 
} else { 
    ps <-readline(prompt="get the password in ") 
} 

options(RCurlOptions = list(
    capath = system.file("CurlSSL", "cacert.pem", 
    package = "RCurl"), ssl.verifypeer = FALSE) 
) 

sheets.con = getGoogleDocsConnection(
    getGoogleAuth("[email protected]", ps, service ="wise")) 

#WARNING: this would prevent curl from detecting a 'man in the middle' attack 
ts2=getWorksheets("hpv type",sheets.con) 

我喜欢使用RStudio。我感到不舒服,因为当时我的办公室里的任何同事都在显示我的密码。我用了一个假密码,但看看图像。 my password would be in plain view for all to see in RStudio。此外,如果我保存了一个工作区,我的密码将与它保存在一起,如果几个月后,当我早已忘记其中的内容时,我就会将它交给其他人,我将我的.RData文件给同事。

我在earlier post里读了关于R中密码的一般信息。在使用RGoogleDocs时,它没有给我足够的信息来隐藏我的密码。

+0

,看到http://stackoverflow.com/a/36218700/3827849 – 2016-04-20 22:50:09

回答

23

我的方法是在R启动文件.Rprofile中的R选项列表 中设置登录名&密码。然后我的代码获得值 与getOption(),然后该值从不可见或将 存储在globalenv()的顶级变量中。 (如果 通过dump.frames进行验尸调试,则可以节省)。

.Rprofile不能被除你以外的任何人阅读是至关重要的。

所以

options(GoogleDocsPassword = c(login = 'password')) 
.Rprofile

,然后

auth = getGoogleAuth() 

只是工作作为第一个参数的默认值是去寻找GoogleDocsPassword选项。

D.

+0

我有点失落。让我们假设登录名是“[email protected]”,密码是“12345”。该怎么会选项(GoogleDocsPassword = c(login ='password'))看。在哪里放行auth = getGoogleAuth()?它是否在Rprofile? – Farrel 2011-05-23 21:41:47

+1

在您的.Rprofile的R外设置GoogleDocsPassword选项后,在任何R会话中,您可以使用 con = getGoogleDocsConnection() 创建连接。关键是这些函数知道如何找到被动地创建连接的登录名和密码。 您甚至可以直接使用 getDocs() ,但使用getGoogleDocsConnection()只创建一次连接并将每个调用传递给更高级别的函数会更有效。 (否则,我们会与Google进行更多沟通,以便每次创建新连接。) – Duncan 2011-05-24 00:12:24

+2

我不会在我的.Rprofile中调用getGoogleAuth()或getGoogleDocsConnection()或任何此类性质的函数。为什么?因为我不会在每个R会话中使用GoogleDocs。我在R会话期间想要连接时调用getGoogleDocsConnection()。在.Rprofile中设置选项只是设置要使用的东西,如果不需要的话不会造成任何问题。 – Duncan 2011-05-24 00:29:18

7

我有同样的问题,并没有真正的解决方案。我使用的解决方法是,我为此创建了一个Google帐户,并带有一个我不关心的密码。然后我分享我希望R用该帐户访问的文档。

但是如果有人对最初的问题有一个答案,我也很感兴趣。

3

似乎你可以在你的选项中存储密码,而不是“ps”直接使用“getOption”。可能有更好的解决方案。

2

这样的事情我一个弥补的电子邮件地址分享谷歌文档,创建一个谷歌帐户,然后用它来共享和授权。因此,将脚本运行所需的个人登录细节分开。

0

用应用程序特定的密码进行2步验证呢? 您可以使用应用程序特定的密码而不会泄露您的真实密码。 ,你可以撤销它,如果你想!

3

你可以存储在你的计算机上的文件的密码,编码和所有与财产以后这样称呼它

getPassword来< - 功能(文件=密码文件的位置){unencode(readlines方法(文件) )}

这一套在你.Rprofile并在代码

getPassword来使用()。

这不会将您的密码存储在任何R文件中,您可以在文件中进行检查。

3

如果你真的不想将它存储在任何地方,那么一个解决方案是不使用密码的变量,甚至可能是谷歌帐户地址!建立在linked answer,如果你使用的是Windows为什么不尝试

library(tcltk) 
library(RGoogleDocs) 

getHiddenText <- function(label = "Enter text:", symbol = "*", defaultText = ""){ 
    wnd <- tktoplevel() 
    entryVar <- tclVar(defaultText) 
    tkgrid(tklabel(wnd, text = label)) 
    #Entry box 
    tkgrid(entryBox <- tkentry(wnd, textvariable = entryVar, show = symbol)) 
    #Hitting return will also submit text 
    tkbind(entryBox, "<Return>", function() tkdestroy(wnd)) 
    #OK button 
    tkgrid(tkbutton(wnd, text="OK", command=function() tkdestroy(wnd))) 
    #Wait for user to submit 
    tkwait.window(wnd) 
    return(tclvalue(entryVar)) 
} 

repeat { 
    con <- try(getGoogleDocsConnection(getGoogleAuth(
     getHiddenText(
      label = "Enter google account:", 
      symbol = "", # or set to "*" to obscure email entry 
      defaultText = "@gmail.com"), # a little timesaver 
     getHiddenText(
      label = "Enter password:", 
      symbol = "*", 
      defaultText = ""), 
     service = "wise"))) 
    if (inherits(con, "try-error")) { 
     userResponse <- tkmessageBox(
      title = "Error", 
      message = "Couldn't connect to Google Docs. Try again?", 
      icon = "error", type = "yesno") 
     if (tclvalue(userResponse) == "no") { 
      stop("Unable to connect to Google Docs, user cancelled.") 
     }   
    } else { # connection successfully authenticated 
     break() # so escape the repeat loop 
    } 
} 
相关问题