R httr post-身份验证下载在交互模式下工作但功能失败
R httr post-authentication download works in interactive mode but fails in function
下面的代码在交互模式下运行良好,但在函数中使用时失败。它非常简单,就是两个身份验证 POST
命令,然后是数据下载。我的目标是让它在一个函数内工作,而不仅仅是在交互模式下。
这个问题有点像 sequel 到 this question.. icpsr 最近更新了他们的网站。下面的最小可重现示例需要一个免费帐户,可在
我尝试添加 Sys.sleep(1)
和各种 httr::GET
/httr::POST
调用,但没有任何效果。
my_download <-
function( your_email , your_password ){
values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)
httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)
tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)
}
# fails
my_download( "email@address.com" , "some_password" )
# stepping through works
debug( my_download )
my_download( "email@address.com" , "some_password" )
EDIT 失败只是下载了这个页面,就好像没有登录一样(而不是数据集),所以它由于某种原因失去了身份验证。如果您已登录 icpsr,请使用隐私浏览查看页面--
https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2?study=21600&ds=1&bundle=rdata&path=ICPSR
谢谢!
这种事情之所以会发生,是因为 httr
包的状态(例如 cookie)存储在每个 URL 的 handle
中(参见 ?handle
)。
在这种特殊情况下,尚不清楚究竟是什么让它起作用,但一种策略是在验证和请求数据之前包含对 https://www.icpsr.umich.edu/cgi-bin/bob/
的 GET
请求。例如,
my_download <-
function( your_email , your_password ){
## for some reason this is required ...
httr::GET("https://www.icpsr.umich.edu/cgi-bin/bob/")
values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)
httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)
}
似乎工作正常,但尚不清楚 GET
对 https://www.icpsr.umich.edu/cgi-bin/bob/ 的请求究竟做了什么或为什么需要它。
下面的代码在交互模式下运行良好,但在函数中使用时失败。它非常简单,就是两个身份验证 POST
命令,然后是数据下载。我的目标是让它在一个函数内工作,而不仅仅是在交互模式下。
这个问题有点像 sequel 到 this question.. icpsr 最近更新了他们的网站。下面的最小可重现示例需要一个免费帐户,可在
我尝试添加 Sys.sleep(1)
和各种 httr::GET
/httr::POST
调用,但没有任何效果。
my_download <-
function( your_email , your_password ){
values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)
httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)
tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)
}
# fails
my_download( "email@address.com" , "some_password" )
# stepping through works
debug( my_download )
my_download( "email@address.com" , "some_password" )
EDIT 失败只是下载了这个页面,就好像没有登录一样(而不是数据集),所以它由于某种原因失去了身份验证。如果您已登录 icpsr,请使用隐私浏览查看页面--
https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2?study=21600&ds=1&bundle=rdata&path=ICPSR
谢谢!
这种事情之所以会发生,是因为 httr
包的状态(例如 cookie)存储在每个 URL 的 handle
中(参见 ?handle
)。
在这种特殊情况下,尚不清楚究竟是什么让它起作用,但一种策略是在验证和请求数据之前包含对 https://www.icpsr.umich.edu/cgi-bin/bob/
的 GET
请求。例如,
my_download <-
function( your_email , your_password ){
## for some reason this is required ...
httr::GET("https://www.icpsr.umich.edu/cgi-bin/bob/")
values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)
httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)
}
似乎工作正常,但尚不清楚 GET
对 https://www.icpsr.umich.edu/cgi-bin/bob/ 的请求究竟做了什么或为什么需要它。