Skip to content

Instantly share code, notes, and snippets.

@efrohnhoefer
Last active July 11, 2016 13:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save efrohnhoefer/716aab14fd5a9c53fc5c to your computer and use it in GitHub Desktop.
Save efrohnhoefer/716aab14fd5a9c53fc5c to your computer and use it in GitHub Desktop.
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