.

Sunday, August 8, 2010

Javascript SDK Example to Connect Your Website to Facebook

id="login">Login
id="logout">Logout
id="disconnect">Disconnect
id="user-info">
src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo/yahoo-min.js&2.7.0/build/event/event-min.js">
id="fb-root">
src="http://connect.facebook.net/en_US/all.js">

// initialize the library with the API key
FB.init({ apiKey: '48f06bc570aaf9ed454699ec4fe416df' });

// fetch the status on load
FB.getLoginStatus(handleSessionResponse);

YAHOO.util.Event.addListener('login', 'click', function() {
FB.login(handleSessionResponse);
});

YAHOO.util.Event.addListener('logout', 'click', function() {
FB.logout(handleSessionResponse);
});

YAHOO.util.Event.addListener('disconnect', 'click', function() {
FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
clearDisplay();
});
});

// no user, clear display
function clearDisplay() {
document.getElementById('user-info').innerHTML = '';
}

// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
// if we dont have a session, just hide the user info
if (!response.session) {
clearDisplay();
return;
}

// if we have a session, query for the user's profile picture and name
FB.api(
{
method: 'fql.query',
query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
},

function(response) {
var user = response[0];
var userInfo = document.getElementById('user-info');
userInfo.innerHTML = '+ user.pic + '">' + user.name;
}
);
}

0 comments:

Post a Comment

Facebook Twitter Delicious Digg More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Macys Printable Coupons