2013-02-23 83 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