Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code demonstrating call to verify_credentials API using OAuth Echo. Written in Scala on the Play Framework.
package controllers
import play.api.libs.ws.WS
import play.api.mvc.{Action, Controller}
import scala.concurrent.Future
class VerifyCredentialsController extends Controller {
implicit val context = scala.concurrent.ExecutionContext.Implicits.global
def verify = Action.async(parse.multipartFormData) { request =>
val urlOpt = request.headers.get("X-Auth-Service-Provider")
val tokenOpt = request.headers.get("X-Verify-Credentials-Authorization")
getTwitterUserId(urlOpt, tokenOpt).map { user =>
user match {
case Some(id) =>
// Do something interesting here here
Ok("Success")
case None => Unauthorized("Failure")
}
}
}
def getTwitterUserId(urlOpt: Option[String], tokenOpt: Option[String]): Future[Option[Long]] = {
(urlOpt, tokenOpt) match {
case (Some(url), Some(token)) => executeVerifyCredentialsResquest(url, token)
case _ => Future.successful(None)
}
}
def executeVerifyCredentialsResquest(url: String, token: String): Future[Option[Long]] = {
WS.url(url).withHeaders("Authorization" -> token).get.map { response =>
response.status match {
case 200 => (response.json \ "id").asOpt[Long]
case _ => None
}
}
}
}
@deepakby1121
Copy link

Hi,

In this code : What are the Some(url) & Some(token) values? and is it urlOpt & tokenOpt that we need to pass in executeVerifyCredentialsResquest?

def getTwitterUserId(urlOpt: Option[String], tokenOpt: Option[String]): Future[Option[Long]] = { (urlOpt, tokenOpt) match { case (Some(url), Some(token)) => executeVerifyCredentialsResquest(url, token) case _ => Future.successful(None) } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment