我一直在寻找解决此问题的方法,但无济于事。我正在验证一个 json 请求。执行见下文。我正在实现 Facebook 的 OAuth2 客户端。根据 Facebook 文档的建议,在收到用户的访问令牌后,您应该“执行自动检查以确认令牌属于您的应用程序期望它属于的人。” 因此,我提出了一个单独的请求,验证细节是否符合预期。以下代码产生编译器错误:“类型不匹配;发现:Inspection.type required: play.api.libs.json.Reads[?]”。有人可以帮我解决这个问题吗?我看到了一个类似的帖子:
创建 Reads for Play 2.1 时出现类型不匹配错误
然而,这并没有帮助我找到解决方案。def inspectAccessToken(json : JsObject) : Future[JsObject] = {
val accessToken = (json \ "access-token").as[String]
// defines a custom reads to be used
def equalReads[T](v: T)(implicit r: Reads[T]): Reads[T] = Reads.filter(ValidationError("validate.error.unexpected.value", v))( _ == v )
def equalDateReads(v : DateTime)(implicit r : Reads[DateTime]) : Reads[DateTime] = Reads.filter(ValidationError("validate.error.unexpected.value", v))( _.toDateMidnight == v.toDateMidnight )
def getDateTime(implicit r : Reads[Int]) : Reads[DateTime] = r.map { d => new DateTime().plusSeconds(d) }
case class Inspection
(
app_id : Int,
application : String,
expires_at : DateTime,
is_valid : Boolean,
user_id : Int
)
implicit val customReads : Reads[Inspection] = (
(__ \ "app_id").read(equalReads(clientId.toInt)) ~
(__ \ "application").read( equalReads("Application")) ~
(__ \ "expires_at").read( getDateTime keepAnd (equalDateReads((json \ "expiry-date").as[DateTime]))) ~
(__ \ "is_valid").read( equalReads(true)) ~
(__ \ "user_id").read[Int]
)(Inspection.apply _)
WS.url(
inspectTokenUrl + "?inputToken=" + accessToken +
"&access_token=" + appToken
).get map { secR =>
val jsonR = secR.json
jsonR.validate(Inspection)
.................
Addl. Code
}
}