Using facebook login for my site
I am trying to include a facebook login button on my home page. I linked it to my application, so I have the application id and secret, but I can't figure out how to check if a user is logged in to my site, so in my database to view his custom file. I used the javaScript sdk from facebook, but when I click the facebook login button, after login, nothing happens and my site home page is displayed again. Does anyone know how to get the user's email address from the javascript sdk? I am using Java Servlets for my pages. I used code from facebook documentation.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '8568345835', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<div class="fb-login-button" data-scope="email">
source to share
https://developers.facebook.com/docs/authentication/
Client flow
The client side thread also uses the OAuth Dialog for user authorization and application authorization. The only difference is that you must specify a parameter response_type
with a token value:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&response_type=token
Just like server side stream, you can also request additional permissions using the scope parameter:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream&
response_type=token
This may be possible, but this is not the best practice. I would suggest introducing some server side scripts (like PHP), but if you are using the Facebook Javascript SDK only to fetch some user data, this method might be helpful. The workflow will be like this.
-
The user clicks the Facebook Connect button, which is redirected to:
https://www.facebook.com/dialog/oauth ? client_id = YOUR_APP_ID & redirect_uri = YOUR_URL & response_type = token
-
Facebook checks YOUR_APP_ID and url and authenticates
-
Redirecting to your URL.
http://YOUR_URL#access_token=166942940015970%7C2.sa0&expires_in=64090
... with an access token in the URL.
Later, you extracted the token from the url and used it to execute FQL (Facebook Query Language) with an AJAX request and get user data.
Described here: https://developers.facebook.com/docs/reference/fql/
source to share