如何使用httr指定证书、密钥和根证书进行基于证书的身份验证?

8

我正在尝试使用httr库从需要证书认证的服务器访问数据。我有证书(cert.pem)、密钥文件(key.pem)和根证书(caroot.pem)。

以下curl命令有效:

curl -H "userName:sriharsha@rpc.com" --cert cert.pem --key certkey.key --cacert caroot.pem https://api.somedomain.com/api/v1/timeseries/klog?limit=1

如何在httr GET请求中指定certkey.key和caroot.pem。我正在尝试以下R命令,但找不到指定cert key和caroot的选项。

cafile=???? r<-GET("https://api.somedomain.com/api/v1/timeseries/klog", query = list(limit = 1), add_headers("userName"= "sriharsha@rpc.com"), config(cainfo = cafile, ssl_verifypeer=FALSE), verbose())

因此,我正在寻找httr的等效选项,用于curl的(--cert、--key和--cacert)。
2个回答

9

特别是,命令行选项“--cacert”对应于配置选项“cainfo”。 - Ott Toomet

1

截至2023年4月,get方法将返回一个错误:

schannel: Failed to import cert file cert.pem, last error is 0x80092002

这似乎与curl 7.55.1中引入的问题有关(请参见https://stackoverflow.com/a/71496170/5956120) 在curl 8.0.1中,使用证书再次有效。

可以在R中实现解决方法:

# request via external tool curl (https://curl.se/windows/)
responseText <- system("<pathToCurl>/curl-8.0.1_6-win64-mingw/bin/curl.exe --cert cert.pem --key certkey.key https://api.somedomain.com/api/v1/timeseries/klog -sS", intern = TRUE)
responseJson <- paste(responseText, collapse = '')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接