我得到一个错误,看起来像:
{"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)
}