Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controllers
- import play.api._
- import play.api.mvc._
- import play.api.libs.ws.WS
- import scala.concurrent.Await
- import scala.concurrent.duration.Duration
- import models.Usuario
- object Application extends Controller {
- implicit val context = scala.concurrent.ExecutionContext.Implicits.global
- val clientID = "310268035408-3tb1r09c44qb4m98eggjggsd2afckh19.apps.googleusercontent.com"
- val clientSecret = "yqAEgKkwaEdxLXJV_Q5pnud6"
- val callbackURL = "http://localhost:9000/googlecallback"
- val loginUrl = s"https://accounts.google.com/o/oauth2/auth?client_id=$clientID&response_type=code&scope=openid+email&redirect_uri=$callbackURL"
- def index = Action {
- Ok(views.html.index(loginUrl))
- }
- def googlecallback = Action.async { request =>
- val optCode: Option[String] = request.getQueryString("code")
- optCode match {
- case None => scala.concurrent.Future { Ok(views.html.index(loginUrl)) }
- case Some(code) =>{
- val params = Map("code" -> Seq(code),
- "client_id" -> Seq(clientID),
- "client_secret" -> Seq(clientSecret),
- "redirect_uri" -> Seq(callbackURL),
- "grant_type" -> Seq("authorization_code"))
- val respTokenFuture = WS.url("https://accounts.google.com/o/oauth2/token").post(params)
- for{
- respToken <- respTokenFuture
- respDados <- {
- val accessToken = (respToken.json \ "access_token").as[String]
- val url = s"https://www.googleapis.com/oauth2/v2/userinfo?access_token=$accessToken"
- WS.url(url).get
- }
- } yield {
- val respJson = respDados.json
- val usuario = new Usuario((respJson \ "picture").as[String], (respJson \ "name").as[String])
- Ok(views.html.pesquisa(usuario))
- }
- }
- }
- }
- def pesquise(origem: String, destino: String) = Action.async { request =>
- val origins = java.net.URLEncoder.encode(origem, "UTF-8")
- val destinations = java.net.URLEncoder.encode(destino, "UTF-8")
- val url = s"http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origins&destinations=$destinations&sensor=false"
- WS.url(url).get.map { resposta =>
- Ok(resposta.json)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement