scala - WS Play Framework(scala)中的“您必须登录”错误
问题描述
我收到如下错误:
{"error":"You must be logged in to complete this action"}
{"error":"You must be logged in to complete this action"}
即使,据推测,我已经通过身份验证,如下面的代码所示。我的代码看起来:
val baseURL = “https://www.dummywebsitename.org”
val authenticationPath = “/ajaxauth/login”
val queryPath = “/basicdomaindata/query/class/tle/ABC/CDE”
val loginURL = baseURL+authenticationPath
val queryURL = baseURL+queryPath
下面,我创建了一个 cookie,然后通过身份验证执行请求,最后尝试从远程 RESTful 服务获取数据。
val cookie = DefaultWSCookie("cookieName", "cookieValue", None, None, Some((2*60*60).toLong), true, true)
val wsRequest = ws.url(loginURL).withAuth(userName, password, WSAuthScheme.BASIC).addCookies(cookie).get()
val wsDataRequest = ws.url(queryURL).post(file)
当我运行代码时,出现上述错误。
解决方案
不确定,但您可能应该使用WSResponse.cookies方法直接从 wsRequest 使用 cookie。
//Not tested. Don't have prepared env :(
val reqLogin = ws.url(loginURL)
.withAuth(userName, password, WSAuthScheme.BASIC)
.addCookies(cookie)
.get()
val wsDataRequest = reqLogin
.filter(_.status == 200)
.flatMap { loginRes =>
//even if we are in Success() branch of Future here, it doesn't mean
//request was made well. It means only we have touched server successfully.
//We still need to check if login was success and so on.
// to see what login result relly is println or check below values:
//loginRes.statusText
//loginRes.status
//loginRes.body
ws.url(queryURL)
.withCookies(loginRes.cookies :_*)
.post(file)
}
推荐阅读
- typescript - 如何在打字稿中重用类方法
- c++ - 引用成员类中的父类 - 包含循环
- wmic - WMIC:当 wmic 命令运行 .txt 文件中的计算机列表以安装程序时,自动回复“Y”
- flutter - 颤振国际 0.17.0-nullsafety.2 包初始化日期格式化
- css - 给出溢出值隐藏了嵌套的sidenav
- python - 仅考虑正元素的列平均值
- javascript - Discord bot 查看登录用户在哪些服务器上
- linux - 如何在不触发OOM杀手的情况下查询内核内存池状态?
- stm32 - STM32 HAL UART接收中断清理缓冲区
- python - update QStyle in EnterEvent