2013-02-23 38 views
5

我問了一個related的問題,並意識到我沒有提出正確的問題(即,這不是關於git)。通過curl命令行發送到github(Windows)

問題是如何在沒有首先使用R在雲中創建項目的情況下將項目推送到github。目前,您可以使用來自this question的信息從RStudio中的git命令行執行此操作。

現在我試圖從Windows機器上將它變成R代碼(Linux很容易)。我被困在第一步,通過R system調用從命令行使用curl。我會顯示我的內容,然後顯示錯誤消息(Thanks to SimonO101 for getting me this far.)。按他的下面我的意見已經編輯很大程度上反映問題,因爲它:

R代碼裏面:

repo <- "New" 
user <- "trinker" 
password <- "password" 

url <- "http://curl.askapache.com/download/curl-7.23.1-win64-ssl-sspi.zip" 
tmp <- tempfile(fileext = ".zip") 
download.file(url,tmp) 
unzip(tmp, exdir = tempdir()) 

system(paste0(tempdir(), "/curl http://curl.haxx.se/ca/cacert.pem -o " , 
    tempdir() , "/curl-ca-bundle.crt")) 

cmd1 <- paste0(tempdir(), "/curl -u '", user, ":", password, 
    "' https://api.github.com/user/repos -d '{\"name\":\"", repo, "\"}'") 

system(cmd1) 

cmd2 <- paste0(tempdir(), "/curl -k -u '", user, ":", password, 
    "' https://api.github.com/user/repos -d '{\"name\":\"", repo, "\"}'") 

system(cmd2) 

錯誤消息(同樣爲這兩種方法):

> system(cmd1) 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 
100 12 0  0 100 12  0  24 --:--:-- --:--:-- --:--:-- 30 
100 47 100 35 100 12  65  22 --:--:-- --:--:-- --:--:-- 83{ 
    "message": "Bad credentials" 
} 

我知道所有文件都存在,因爲:

> dir(tempdir()) 
[1] "curl-ca-bundle.crt" "curl.exe"    "file1aec62fa980.zip" "file1aec758c1415.zip" 

它不能是我的密碼或用戶名,因爲這個工程在Linux Mint的(唯一的區別是捲曲之前的部分):

repo <- "New" 
user <- "trinker" 
password <- "password" 


cmd1 <- paste0("curl -u '", user, ":", password, 
    "' https://api.github.com/user/repos -d '{\"name\":\"", repo, "\"}'") 

system(cmd1) 

注:Windows 7機器。 [R 2.14.1

+0

這個呢,因爲你建議在我的Mac上正常工作。錯誤提示您是否嘗試過'-k'選項?即'cmd1 < - paste0(「curl -k -u'」,用戶,「:」,密碼, 「'https://api.github.com/user/repos -d'{\」name \「: \「」,回購,「\」}'「)' – 2013-02-23 16:17:50

+0

是的,它似乎無限期掛斷。我會再試一次。 – 2013-02-23 16:18:18

+0

啊,它也掛了,但我不小心關上了我的MBP蓋子。當我重新打開它時,命令已經完成,並且回購已經出現在GitHub上!去搞清楚。 – 2013-02-23 16:19:25

回答

4

編輯 - OP提供的賞金

確定後,事實證明這是一些瘋狂的窗戶字符轉義在命令行上做。基本上問題是我們將格式不正確的json請求傳遞給github。

您可以使用shQuote正確地格式化Windows的curl請求的違規部分。我們可以測試平臺類型,看看我們是否需要包括適用於Windows的情況下的特殊格式,像這樣:

repo <- "NewRepository" 
json <- paste0(" { \"name\":\"" , repo , "\" } ") #string we desire formatting 
os <- .Platform$OS.type #check if we are on Windows 
if(os == "windows"){ 
json <- shQuote(json , type = "cmd") 
cmd1 <- paste0(tempdir() ,"/curl -i -u \"" , user , ":" , password , "\" https://api.github.com/user/repos -d " , json) 
} 

這個工作在我的Windows 7盒沒有任何問題。如果你願意,我可以更新GitHub腳本嗎?

OLD ANSWER

我做了一些周圍挖掘herehere,它可能是回答你的問題是更新捲曲-CA束。它可以幫助Windows讓R使用internet2.dll

repo <- "New" 
user <- "trinker" 
password <- "password" 

url <- "http://curl.askapache.com/download/curl-7.23.1-win64-ssl-sspi.zip" 
tmp <- tempfile(fileext = ".zip") 
download.file(url,tmp) 
unzip(tmp, exdir = tempdir()) 
system(paste0("curl http://curl.haxx.se/ca/cacert.pem -o " , tempdir() , "/curl-ca-bundle.crt")) 
system(paste0(tempdir(),"/curl", " -u \'USER:PASS\' https://api.github.com/user/repos -d \'{\"name\":\"REPO\"}\'")) 

再次,我無法測試這個,因爲我沒有訪問我的Windows機器,但更新證書頒發機構文件似乎幫助了其他一些人。從curl website,捲曲的Windows版本應該尋找curl-ca-bundle.crt文件按以下順序:

  1. 應用程序的目錄
  2. 當前工作目錄
  3. Windows系統目錄(如C:\ Windows \ System32下)
  4. Windows目錄(如C:\ WINDOWS)
  5. 沿%PATH%中的所有目錄
+0

輝煌的+1和+300的賞金 – 2013-02-26 18:10:01

+0

@TylerRinker Yay。非常感謝你,先生。 – 2013-02-26 18:21:21

+0

我將此功能歸功於您。如果您希望我使用您的真實姓名,請通過github提供的電子郵件地址向我發送電子郵件。再次感謝你的幫助。 – 2013-02-26 18:25:22

相關問題