2011-05-23 23 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 
    } 
} 
相關問題