Created
December 12, 2011 23:08
-
-
Save 0xRoch/1469572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import java.io.StringReader | |
import javax.xml.parsers.DocumentBuilder | |
import javax.xml.parsers.DocumentBuilderFactory | |
import models.Contact | |
import org.w3c.dom.Document | |
import org.xml.sax.InputSource | |
import play.cache.Cache | |
import play.libs.OAuth | |
import play.libs.OAuth.ServiceInfo | |
import play.libs.OAuth.TokenPair | |
import play.libs.WS | |
import play.mvc.Scope.Session | |
import play.modules.gae._ | |
class Oauth extends Application { | |
} | |
object Oauth { | |
def googleConnect():Unit = { | |
if (GAE.isLoggedIn()) { | |
if (Contact.findByEmail(GAE.getUser().getEmail()) == null) { | |
var user:Contact = new Contact(GAE.getUser().getEmail()) | |
user.insert() | |
Session.current().put("logged", user.id) | |
redirect("/") | |
} else { | |
var user:Contact = Contact.findByEmail(GAE.getUser().getEmail()) | |
Session.current().put("logged", user.id) | |
redirect("/") | |
} | |
} else { | |
GAE.login("Oauth.googleConnect") | |
} | |
} | |
def twitterConnect():Unit = { | |
if (OAuth.isVerifierResponse()) { | |
var tokens:TokenPair = OAuth.service(TWITTER).requestAccessToken(new TokenPair(Cache.get("token_token" + session.getId()).asInstanceOf[String] , Cache.get("token_secret" + session.getId()).asInstanceOf[String] )) | |
var url:String = "http://api.twitter.com/1/account/verify_credentials.xml" | |
var accInfo:String = "" | |
try { | |
accInfo = WS.url(url).oauth(TWITTER, tokens).get().getString() | |
} catch { | |
case e:Exception => | |
Application.index() | |
} | |
try { | |
var dbf:DocumentBuilderFactory = DocumentBuilderFactory.newInstance() | |
var db:DocumentBuilder = dbf.newDocumentBuilder() | |
var is:InputSource = new InputSource() | |
is.setCharacterStream(new StringReader(accInfo)) | |
var doc:Document = db.parse(is) | |
var twitterId:String = doc.getElementsByTagName("id").item(0).getTextContent() | |
var twitter:String = doc.getElementsByTagName("name").item(0).getTextContent() | |
if (Application.connectedUser() == null) { | |
if (Contact.findByTwitterId(Long.parseLong(twitterId)) == null) { | |
var user:Contact = new Contact(twitter, Long.parseLong(twitterId)) | |
user.insert() | |
Session.current().put("logged", user.id) | |
} else { | |
var user:Contact = Contact.findByTwitterId(Long.parseLong(twitterId)) | |
Session.current().put("logged", user.id) | |
} | |
Application.index() | |
} else { | |
Application.index() | |
} | |
} catch { | |
case e:Exception => | |
play.Logger.error("error parsing twitter login result", e) | |
} | |
} | |
var twitt:OAuth = OAuth.service(TWITTER) | |
var tokens:TokenPair = twitt.requestUnauthorizedToken() | |
Cache.set("token_secret" + session.getId(), tokens.secret, "15min") | |
Cache.set("token_token" + session.getId(), tokens.token, "15min") | |
redirect(twitt.redirectUrl(tokens)) | |
} | |
private val TWITTER:ServiceInfo = new ServiceInfo("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize", "oLfakefakefakehVXz8ZriA", "4SOyKjUlkfakefakefake1JnjWXYoaiY1sVN7cSsc") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment