如何将 cookie 与 RCurl 一起使用?
我正在尝试编写一个 R 包,通过 REST API 访问一些数据。然而,该 API 不使用 http 身份验证,而是依赖 cookie 来保存会话凭据。
本质上,我想用两个 R 函数替换 bash 脚本中的以下两行:一个用于执行登录并存储会话 cookie,第二个用于获取数据。
curl -X POST -c cookies.txt -d"username=xxx&password=yyy" http://api.my.url/login
curl -b cookies.txt http://api.my.url/data
我显然不明白 RCurl 如何与curl 选项一起使用。我的脚本目前的情况是:
library(RCurl)
curl <- getCurlHandle()
curlSetOpt(cookiejar='cookies.txt', curl=curl)
postForm("http://api.my.url/login", username='xxx', password='yyy', curl=curl)
getURL('http://api.my.url/data", curl=curl)
最终的 getURL() 失败并显示“未登录”。来自服务器的消息,并且在 postForm()
之后不存在 cookies.txt
文件。
I am trying to write an R package that accesses some data via a REST API. The API, however, doesn't use http authentication, but rather relies on cookies to keep credentials with the session.
Essentially, I'd like to replace the following two lines from a bash script with two R functions: One to perform the login, and store the session cookie, and the second to GET the data.
curl -X POST -c cookies.txt -d"username=xxx&password=yyy" http://api.my.url/login
curl -b cookies.txt http://api.my.url/data
I'm clearly not understanding how RCurl works with curl options. My script as it stands has:
library(RCurl)
curl <- getCurlHandle()
curlSetOpt(cookiejar='cookies.txt', curl=curl)
postForm("http://api.my.url/login", username='xxx', password='yyy', curl=curl)
getURL('http://api.my.url/data", curl=curl)
The final getURL()
fails with a "Not logged in." message from the server, and after the postForm()
no cookies.txt
file exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说你不需要创建cookie文件,除非你想研究cookie。
鉴于此,实际上,网络服务器使用代理数据、重定向和隐藏的帖子数据,但这应该有所帮助:
重要
除了用户名和密码之外,通常还可能存在隐藏的帖子数据。要捕获它,您可能需要(例如在 Chrome 中)使用
开发人员工具
(Ctrl Shift I) -> ;Network Tab
,用于显示帖子字段名称和值。In general you don't need to create a cookie file, unless you want to study the cookies.
Given this, in real word, web servers use agent data, redirecting and hidden post data, but this should help:
Important
There can often be hidden post data, beyond username and password. To capture it you may want, e.g. in Chrome, to use
Developer tools
(Ctrl Shift I) ->Network Tab
, in order to show the post field names and values.我的不好。 Neal Richter 向我指出http://www.omegahat.org/RCurl/RCurlJSS.pdf - 更好地解释了
cookiefile
和cookiejar
之间的区别。问题中的示例脚本实际上确实工作。但它仅在不再使用时将文件写入磁盘。My bad. Neal Richter pointed out to me http://www.omegahat.org/RCurl/RCurlJSS.pdf - which better explains the difference between
cookiefile
andcookiejar
. The sample script in the question actually does work. But it only writes the file to disk when it is no longer being used.