通过curl命令行(Windows)发送到github [英] Send to github via curl command line (Windows)

查看:600
本文介绍了通过curl命令行(Windows)发送到github的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个相关问题,发现我不是在问正确的问题(即,这不是关于git)。



问题是如何将项目推送到github,而无需首先在云中使用R创建项目。您可以使用此问题中的信息从RStudio中的git命令行执行此操作。 / p>

现在我试图把它变成R代码现在从Windows机器(Linux很容易)。我停留在第一步使用curl从命令行通过R 系统调用。我将显示我的内容,然后显示错误消息(感谢SimonO101让我这么远。)。根据他在下面的评论,我已经大量编辑以反映问题:



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 u,user,:,password,
'https://api.github.com/user/repos -d'{\name\:\,repo, \}'')

系统(cmd2)

strong>错误讯息(两种方法都相同):

 系统(cmd1)
%总接收百分比%Xferd平均速度时间时间时间当前
载入上传总剩余速度

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.crtcurl.exefile1aec62fa980.zipfile1aec758c1415.zip
pre>

它不能是我的密码或用户名,因为这在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)
pre>

注意:Windows 7机器。 R 2.14.1



好吧,原来是在命令行上做一些疯狂的窗口字符转义。基本上问题是我们传递格式不正确的json请求到github。



您可以使用 shQuote off的curl请求的部分。我们可以测试平台类型,看看我们是否需要包括特殊的格式为Windows的情况下:

  repo< 
json< - paste0({\name\:\,repo,\})#string我们希望格式化
os< - .Platform $ OS.type #check如果我们在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脚本吗?



旧答案



曾在此处此处,这可能是你的问题的答案是更新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())
系统(paste0(curl http://curl.haxx.se/ca/cacert.pem -o,tempdir(),/curl-ca-bundle.crt))
系统(paste0 tempdir(),/ curl,-u \'USER:PASS\'https://api.github.com/user/repos -d \'{\name\:\ REPO\} \'))

我没有访问我的Windows框,但更新的证书颁发机构文件似乎已帮助了一些其他人。从 curl网站,Windows版本的curl应该查找 curl-ca-bundle.crt 文件:


  1. 应用程序的目录

  2. 当前工作目录

  3. Windows系统目录(例如C:\windows \system32)

  4. Windows目录:\windows)

  5. 沿%PATH%的所有目录


I asked a related question and realized I wasn't asking the right question (i.e., this isn't about git).

The question is how to push a project to github without first creating the project in the clouds using R. Currently you can do this from the git command line in RStudio using info from this question.

Now I'm trying to make that into R code now from a Windows machine (Linux was easy). I'm stuck at the first step using curl from the command line via an R system call. I'll show what I have and then the error message (Thanks to SimonO101 for getting me this far.). Per his comments below I've edited heavily to reflect the problem as it:

R Code:

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)

Error Messages (same for both approaches):

> 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"
}

I know all the files are there because:

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

It can't be my password or user name because this works on Linux Mint (the only difference is the part before curl):

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


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

system(cmd1)

NOTE: Windows 7 machine. R 2.14.1

解决方案

EDIT - After OP offered bounty

Ok, it turns out it is to do with some crazy windows character escaping on the command line. Essentially the problem was we were passing improperly formatted json requests to github.

You can use shQuote to properly format the offending portion of the curl request for Windows. We can test platform type to see if we need to include special formatting for Windows cases like so:

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 )
}

This worked on my Windows 7 box without any problems. I can update the GitHub script if you want?

OLD ANSWER

I did some digging around here and here and it might be that the answer to your problem is to update the curl-ca-bundle. It may help on Windows to get R to use the 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\"}\'") )

Again, I can't test this as I don't have access to my Windows box, but updating the certificate authority file seems to have helped a few other people. From the curl website, the Windows version of curl should look for the curl-ca-bundle.crt file in the following order:

  1. application's directory
  2. current working directory
  3. Windows System directory (e.g. C:\windows\system32)
  4. Windows Directory (e.g. C:\windows)
  5. all directories along %PATH%

这篇关于通过curl命令行(Windows)发送到github的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆