使用POST&;在R中具有CSFR令牌的HTTR

使用POST&;在R中具有CSFR令牌的HTTR,r,R,我正试图使用POST函数通过httr库访问R中的以下URL。我需要拿出一些已经开发好脚本的货币汇率。我在验证工具中的不同时间段时遇到问题 https://www.oanda.com/fx-for-business/historical-rates/auth/login/ 我正在使用以下代码 library(httr) Cookie = paste('opc_id=599C877C-7B4C-11E7-A634-FD6593E2D6B9;', 'csrftoken=EJqI

我正试图使用POST函数通过httr库访问R中的以下URL。我需要拿出一些已经开发好脚本的货币汇率。我在验证工具中的不同时间段时遇到问题

https://www.oanda.com/fx-for-business/historical-rates/auth/login/
我正在使用以下代码

  library(httr)

        Cookie = paste('opc_id=599C877C-7B4C-11E7-A634-FD6593E2D6B9;', 'csrftoken=EJqIgiufV77Nlv4E54mKoFcxhKGJgnO4cdQzUy2ehaayjD5FX21F8MznpHm0o4W5;','sessionid "6bc5s0qnswsue0pxe3st83ezf6m9cdl5;')

    headers = list (
    Host= "www.oanda.com",
        "User-Agent" = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
         Accept =  "*/*",
        "Accept-Language" = "en-US,en;q=0.5",
        "Accept-Encoding" =  "gzip, deflate, br",
        "Content-Type"= "application/x-www-form-urlencoded; charset=UTF-8",
        Referer =  "https://www.oand

a.com/fx-for-business/historical-rates",
    "Content-Length" = 64,
    "X-Requested-With" =  "XMLHttpRequest",
    Cookie = Cookie,    Connection= "keep-alive")

    .headers = character(headers)

    par = list (
    email = "user",
    password = "pass",
    redirect=""
    )

    POST("https://www.oanda.com/fx-for-business/historical-rates/auth/login/",body = pars, add_headers(.headers))
最后一行的结果如下。脚本需要CSFR令牌,如何生成它

        <h1>Forbidden 
            <span>(403)</span>
        </h1>
        <p>CSRF verification failed. Request aborted.</p>
        <p>You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p>
        <p>If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for &#39;same-origin&#39; requests.</p>

解决方案在于模拟R中的web浏览器。 实现这一点的代码如下所示

GET https://www.oanda.com/static/sfb/hcc/templates/html/en/popup.login.html?_=1502097958941
    POST https://www.oanda.com/fx-for-business/historical-rates/auth/login/
    GET https://www.oanda.com/fx-for-business/historical-rates/getUser/?_=1502097958942
    GET https://www.oanda.com/fx-for-business/historical-rates/message/messages/?_=1502097958943
    GET https://www.oanda.com/static/sfb/hcc/templates/html/en/banner.expiry.html?_=1502097958944
    GET https://www.oanda.com/static/sfb/hcc/templates/html/en/popup.login.success.html?_=1502097958945
    library(wdman) #I don't  know if this library is necessary
    library(RSelenium)

    selServ <- selenium(jvmargs = c("-Dwebdriver.chrome.verboseLogging=true"))
    remDr <- remoteDriver(port = 4567L, browserName = "chrome")
    remDr$open()

    ##From here you need to access with your login details inside the browser

    remDr$navigate(url)


    df=remDr$getPageSource()
library(wdman)#我不知道这个库是否必要
图书馆(资源库)

selServ您也可以在headless模式下使用chrome(当前为unix和OSX)参见此处的第二种方法