Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created December 12, 2011 16:18
Show Gist options
  • Save dhavaln/1468072 to your computer and use it in GitHub Desktop.
Save dhavaln/1468072 to your computer and use it in GitHub Desktop.
Facebook JS SDK integration with Play Framework
public static void facebookLogin() {
try {
JsonObject profile = FbGraph.getObject("me"); // fetch the logged in user
String email = profile.get("email").getAsString(); // retrieve the email
System.out.println("facebook user " + email);
// do useful things
Session.current().put("username", email); // put the email into the session (for the Secure module)
} catch (FbGraphException fbge) {
flash.error(fbge.getMessage());
if (fbge.getType() != null && fbge.getType().equals("OAuthException")) {
Session.current().remove("username");
}
}
redirect("/");
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:fb="http://ogp.me/ns/fb#">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
</script>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
console.log("init is done");
FB.getLoginStatus(function(response){
console.log(response);
});
// Additional initialization code here
FB.Event.subscribe('auth.authResponseChange', function(response) {
alert('The status of the session is: ' + response.status);
});
FB.Event.subscribe('auth.login', function(response) {
alert('user is logged in: ' );
});
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=220999597976156";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<fb:login-button scope="email" onlogin="facebookLogin();"></fb:login-button>
<script type="text/javascript">
function facebookLogin() {
// get current login status from facebook.com
FB.getLoginStatus(function(response) {
console.log(response);
if (response.authResponse) {
// logged in and connected user, someone you know
window.location = "@{Application.facebookLogin()}";
} else {
//window.location = "@{Secure.logout()}";
}
});
FB.Event.subscribe('auth.login', function(response) {
// do something with response
console.log("auth logged in ");
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment