windows - 通过 curl 命令行发送到 github (Windows)

标签 windows r curl github

我问了一个related问题并意识到我没有问正确的问题(即,这与 git 无关)。

问题是如何在不首先使用 R 在云中创建项目的情况下将项目推送到 github。目前,您可以使用来自 this question 的信息从 RStudio 中的 git 命令行执行此操作。 .

现在我正尝试从 Windows 机器(Linux 很容易)将其转换为 R 代码。我停留在第一步,通过 R system 调用从命令行使用 curl。我将展示我拥有的内容,然后展示错误消息 ( Thanks to SimonO101 for getting me this far. )。根据他在下面的评论,我进行了大量编辑以反射(reflect)问题:

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(唯一的区别是 curl 之前的部分):

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

最佳答案

编辑 - 在 OP 提供赏金之后

好吧,事实证明这是与命令行上一些疯狂的 Windows 字符转义有关。本质上,问题是我们向 github 传递了格式不正确的 json 请求。

您可以使用 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 脚本吗?

旧答案

我在 here 周围做了一些挖掘工作和 here您的问题的答案可能是更新 curl-ca-bundle。在 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 应按以下顺序查找 curl-ca-bundle.crt 文件:

  1. 应用目录
  2. 当前工作目录
  3. Windows 系统目录(例如 C:\windows\system32)
  4. Windows 目录(例如 C:\windows)
  5. %PATH% 沿线的所有目录

关于windows - 通过 curl 命令行发送到 github (Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042418/

相关文章:

c++ - 对于中高级 C# 开发人员开始使用 Visual Studio 2010 C++ 有哪些好的引用资料?

c++ - 如何在 Windows 中获取当前登录用户的域?

java - Java 中的 cURL 删除

java - wget/curl 从 Oracle Archive 页面下载 Oracle Java 7 SDK

c++ - 如何打开分区句柄以获取其在动态磁盘上的信息?

c++ - Windows 样式标志(如 WS_TILED 和 WS_ICONIC 只是重命名其他标志)的目的是什么? ( Windows/C++)

在R中 reshape 数据框

r - LaTeX 和knitr - 插入 R 代码时出错

r - 有没有办法用 R 将 html 代码嵌入到工具提示中?

rest - 在 Azure 管道任务中运行 CURL GET 命令