Some days ago facebook released their new graph api system to develop social sites more easily. Its really cool and I personally like the facebook's new graph api system. Facebook also updated their old facebook connect system and provided better way to integrate facebook connect feature, user authentication and calling facebook api from website.
In this article, I'll highlight javascript base
- Facebook connect authentication for websites
- How to use graph api
- How to show stream publish prompt and share prompt
- How to run FQL query using javascript
- How to set status using old legacy api calling by javascript
Here is the demo http://thinkdiff.net/demo/newfbconnect1/newconnect.php
1. Facebook connect authentication Firstly you've to setup a facebook application to get the application id. You can setup application from here. Or if you have already setup a facebook application then just copy the application id and replace with xxxxxxxxxxx.
01 | <div id= "fb-root" ></div> |
02 | <script type= "text/javascript" > |
03 | window.fbAsyncInit = function () { |
04 | FB.init({appId: 'xxxxxxxxxxxxxx' , status: true , cookie: true , xfbml: true }); |
07 | var e = document.createElement( 'script' ); |
08 | e.type = 'text/javascript' ; |
09 | e.src = document.location.protocol + |
10 | '//connect.facebook.net/en_US/all.js' ; |
12 | document.getElementById( 'fb-root' ).appendChild(e); |
Just put this code in your html file after the body tag. This is the most efficient way to load the javascript SDK in your site. And in this way the sdk will load asynchronously so it does not block loading other elements of your page. Details of FB.init method.
And your html tag should be
Now we will use another method FB.Event.subscribe so that we can subscribe some events like login, logout, sessionChange. So modify the window.fbAsynInit and update by below code
01 | window.fbAsyncInit = function () { |
02 | FB.init({appId: 'xxxxxxxxxxx' , status: true , cookie: true , xfbml: true }); |
05 | FB.Event.subscribe( 'auth.login' , function (response) { |
09 | FB.Event.subscribe( 'auth.logout' , function (response) { |
14 | FB.getLoginStatus( function (response) { |
15 | if (response.session) { |
So when user will login first time (not automatically) login() method will call. When user will click logout , logout() method will call. So define these functions for your application purpose. We also use another method FB.getLoginStatus() within window.fbAsyncInit() function so that when we found user is logged in or connected we will show some info to user.
Now we have to render fb connect button so that user can login or logout.
1 | < fb:login-button autologoutlink = "true" perms = "email,user_birthday,status_update,publish_stream" ></ fb:login-button > |
If you don't need any extended permissions from user then remove perms="email,user_birthday,status_update,publish_stream" this line. If you don't want to use standard fbconnect button for login and logout you can define your own connect button. Checkout FB.login for login javascript method and FB.logout for logout javascript method.
2. How to use graph api
To fully understand graph api visit the link . Now i'll show how you can use graph api using javascript sdk. There is a method FB.api . This method directly hooks graph api and returns result. Sample code
1 | FB.api( '/me' , function (response) { |
By calling this method it will show logged in user name.
In my demo you'll see another customized method graphStreamPublish(). This method uses FB.api() and does a HTTP POST request to http://graph.facebook.com/me/feed url with message parameter. So that the message will publish to users own wall.
01 | function graphStreamPublish(){ |
02 | var body = 'Reading New Graph api & Javascript Base FBConnect Tutorial' ; |
03 | FB.api( '/me/feed' , 'post' , { message: body }, function (response) { |
04 | if (!response || response.error) { |
05 | alert( 'Error occured' ); |
07 | alert( 'Post ID: ' + response.id); |
3. How to show stream publish prompt and share prompt
There is another javascript sdk method named FB.ui. Using the code you can prompt user for stream publish or share your page. Checkout streamPublish() and share() methods defination in my demo's source code .
4. How to run FQL query using javascript
Facebook Query Language enables you to use a SQL-style interface to query the data exposed by the Graph API. It provides for some advanced features not available in the Graph API, including batching multiple queries into a single call. Checkout the tables to know which data of facebook user you can retrieve. In the demo's source code you'll see
02 | FB.api( '/me' , function (response) { |
03 | var query = FB.Data.query( 'select name, hometown_location, sex, pic_square from user where uid={0}' , response.id); |
04 | query.wait( function (rows) { |
06 | document.getElementById( 'name' ).innerHTML = |
07 | 'Your name: ' + rows[0].name + "<br />" + |
08 | '<img src="' + rows[0].pic_square + '" alt="" />' + "<br />" ; |
This method used the sdk method FB.Data.query to run FQL. Checkout the link for a complex query example.
5. How to set status using old legacy api calling by javascript
You can still call the old legacy api. In my demo you'll see a text box, write something and click 'Status Set Using Legacy Api Call' You'll see your facebook status will update.
02 | status1 = document.getElementById( 'status' ).value; |
10 | alert( 'Your facebook status not updated. Give Status Update Permission.' ); |
13 | alert( 'Your facebook status updated' ); |
But remember to run successfully the demo, update status and publish stream using graph api you must approved all exteneded permissions those will show you during first access to the application.
Hope this article will help you to make facebook connected social site.
Here is my demo's full source code:
004 | <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" /> |
005 | <title>New Graph api & Javascript Base FBConnect Tutorial | Thinkdiff.net</title> |
008 | <div id= "fb-root" ></div> |
009 | <script type= "text/javascript" > |
010 | window.fbAsyncInit = function () { |
011 | FB.init({appId: '113700398662301' , status: true , cookie: true , xfbml: true }); |
014 | FB.Event.subscribe( 'auth.login' , function (response) { |
018 | FB.Event.subscribe( 'auth.logout' , function (response) { |
023 | FB.getLoginStatus( function (response) { |
024 | if (response.session) { |
031 | var e = document.createElement( 'script' ); |
032 | e.type = 'text/javascript' ; |
033 | e.src = document.location.protocol + |
034 | '//connect.facebook.net/en_US/all.js' ; |
036 | document.getElementById( 'fb-root' ).appendChild(e); |
040 | FB.api( '/me' , function (response) { |
041 | document.getElementById( 'login' ).style.display = "block" ; |
042 | document.getElementById( 'login' ).innerHTML = response.name + " succsessfully logged in!" ; |
046 | document.getElementById( 'login' ).style.display = "none" ; |
050 | function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){ |
053 | method: 'stream.publish' , |
058 | description: (description), |
062 | { text: hrefTitle, href: hrefLink } |
064 | user_prompt_message: userPrompt |
071 | function showStream(){ |
072 | FB.api( '/me' , function (response) { |
074 | streamPublish(response.name, 'Thinkdiff.net contains geeky stuff' , 'hrefTitle' , 'http://thinkdiff.net' , "Share thinkdiff.net" ); |
080 | method: 'stream.share' , |
084 | FB.ui(share, function (response) { console.log(response); }); |
087 | function graphStreamPublish(){ |
088 | var body = 'Reading New Graph api & Javascript Base FBConnect Tutorial' ; |
089 | FB.api( '/me/feed' , 'post' , { message: body }, function (response) { |
090 | if (!response || response.error) { |
091 | alert( 'Error occured' ); |
093 | alert( 'Post ID: ' + response.id); |
Po recommended