"You must be logged in" WS Play Framework (scala) 错误
"You must be logged in" error in 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)
}
我收到如下错误:
{"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)
}