how to authenticate a shibboleth multi-hostname website with httr in R

后端 未结 2 1919
野性不改
野性不改 2021-02-05 20:17

note: ipums international and ipums usa probably use the same system. ipums usa allows quicker signup. if you would like to test out your code, try https://usa.ipums.org/usa-a

2条回答
  •  长发绾君心
    2021-02-05 20:59

    You have to use set_cookies() to send your cookies to the server:

    library(httr)
    library(rvest)
    #my_email <- "xxx"
    #my_password <- "yyy"
    tf <- tempfile()
    set_config( config( ssl_verifypeer = 0L ) )
    
    # Get first page
    p1 <- GET( "https://international.ipums.org/international-action/users/login" , verbose( info = TRUE ) )
    
    # Post Login credentials
    b2 <- list( "j_username" = my_email , "j_password" = my_password )
    c2 <- c(JSESSIONID=p1$cookies[p1$cookies$domain=="#HttpOnly_live.identity.popdata.org",]$value,
               `_idp_authn_lc_key`=p1$cookies[p1$cookies$domain=="live.identity.popdata.org",]$value)
    p2 <- POST(p1$url,body = b2, set_cookies(.cookies = c2), encode="form" )
    
    # Parse hidden fields
    h2 <- read_html(p2$content)
    form <-  h2 %>% html_form() 
    
    # Post hidden fields
    b3 <- list( "RelayState"=form[[1]]$fields[[1]]$value, "SAMLResponse"=form[[1]]$fields[[2]]$value)
    c3 <- c(JSESSIONID=p1$cookies[p1$cookies$domain=="#HttpOnly_live.identity.popdata.org",]$value,
               `_idp_session`=p2$cookies[p2$cookies$name=="_idp_session",]$value,
               `_idp_authn_lc_key`=p2$cookies[p2$cookies$name=="_idp_authn_lc_key",]$value)
    p3 <- POST( form[[1]]$url , body=b3, set_cookies(.cookies = c3), encode = "form")
    
    # Get interesting page
    c4 <- c(JSESSIONID=p3$cookies[p1$cookies$domain=="international.ipums.org" && p3$cookies$name=="JSESSIONID",]$value,
               `_idp_session`=p3$cookies[p3$cookies$name=="_idp_session",]$value,
               `_idp_authn_lc_key`=p3$cookies[p3$cookies$name=="_idp_authn_lc_key",]$value)
    p4 <- GET( "https://international.ipums.org/international-action/menu", set_cookies(.cookies = c4) )
    writeBin(p4$content , tf )
    readLines( tf )[55]
    

    Since the result is

    [1] "    
  • Logout
  • "

    I think you're logged in...

提交回复
热议问题