Advertisement
Guest User

Untitled

a guest
Jun 5th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.20 KB | None | 0 0
  1. package controllers
  2.  
  3. import play.api._
  4. import play.api.mvc._
  5. import play.api.libs.ws.WS
  6. import scala.concurrent.Await
  7. import scala.concurrent.duration.Duration
  8. import models.Usuario
  9.  
  10. object Application extends Controller {
  11.  
  12.   implicit val context = scala.concurrent.ExecutionContext.Implicits.global
  13.   val clientID = "310268035408-3tb1r09c44qb4m98eggjggsd2afckh19.apps.googleusercontent.com"
  14.   val clientSecret = "yqAEgKkwaEdxLXJV_Q5pnud6"
  15.   val callbackURL = "http://localhost:9000/googlecallback"
  16.   val loginUrl = s"https://accounts.google.com/o/oauth2/auth?client_id=$clientID&response_type=code&scope=openid+email&redirect_uri=$callbackURL"
  17.    
  18.   def index = Action {
  19.     Ok(views.html.index(loginUrl))
  20.   }
  21.  
  22.   def googlecallback = Action.async { request =>
  23.     val optCode: Option[String] = request.getQueryString("code")
  24.     optCode match {
  25.       case None => scala.concurrent.Future { Ok(views.html.index(loginUrl)) }
  26.       case Some(code) =>{
  27.           val params = Map("code" -> Seq(code),
  28.                            "client_id" -> Seq(clientID),
  29.                            "client_secret" -> Seq(clientSecret),
  30.                            "redirect_uri" -> Seq(callbackURL),
  31.                            "grant_type" -> Seq("authorization_code"))
  32.           val respTokenFuture = WS.url("https://accounts.google.com/o/oauth2/token").post(params)
  33.  
  34.             for{
  35.                 respToken <- respTokenFuture
  36.                 respDados <- {
  37.                     val accessToken = (respToken.json \ "access_token").as[String]
  38.                     val url = s"https://www.googleapis.com/oauth2/v2/userinfo?access_token=$accessToken"
  39.                     WS.url(url).get
  40.                 }
  41.             } yield {
  42.                 val respJson = respDados.json
  43.                 val usuario = new Usuario((respJson \ "picture").as[String], (respJson \ "name").as[String])
  44.                 Ok(views.html.pesquisa(usuario))
  45.             }
  46.         }
  47.       }
  48.   }
  49.  
  50.   def pesquise(origem: String, destino: String) = Action.async { request =>
  51.         val origins = java.net.URLEncoder.encode(origem, "UTF-8")
  52.         val destinations = java.net.URLEncoder.encode(destino, "UTF-8")
  53.         val url = s"http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origins&destinations=$destinations&sensor=false"
  54.    
  55.         WS.url(url).get.map { resposta =>
  56.           Ok(resposta.json)
  57.         }
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement