jueves, 17 de noviembre de 2011

How to use Facebook Graph API with Javascript

For this example I'm using: MacBook Pro with Lion(10.7.1) as operative system, Netbeans 7.0 and a Facebook account.

This is a quick example of Facebook Graph API. This code allows Facebook users to publish information in their walls, retrieve friends and send message to their friends. To test this code you have to create a Facebook application in https://developers.facebook.com/apps
I named the application as TestGraphAPI:
Enter the captcha value:

Then you have to register the URL of your application, pg: http://localhost:8080/TestGraphAPI.
I created a web java application in Netbeans and pasted the following code in the index jsp page:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Facebook Example</title>
    </head>
    <body>
        <div id="fb-root">
            <script type="text/javascript">

                window.fbAsyncInit = function() {
                    FB.init({appId: 'XXXXXXXXXXXXXXX', status: true, cookie: true, xfbml: true});
                    FB.getLoginStatus(function(response) {
                        if (response.session!=null) {
                            alert("Welcome, you have been logged to Facebook");
                        }else{
                            alert("To use this application you have to be logged at Facebook");
                     
                        }
                    });
                };
                (function() {
                    var scriptFacebook = document.createElement('script');
                    scriptFacebook.type = 'text/javascript';
                    scriptFacebook.src = document.location.protocol +
                        '//connect.facebook.net/en_US/all.js';
                    scriptFacebook.async = true;
                    document.getElementById('fb-root').appendChild(scriptFacebook);
                }());
         
                function obtenerAmigos(){
                    document.getElementById("imagenes").innerHTML="Click on the images to send a message<br/>";
                    FB.api('/me/friends',function(response){
                        for(i=0;i<response.data.length;i++){
                            document.getElementById("imagenes").innerHTML+="<img src='https://graph.facebook.com/"+response.data[i].id+"/picture' onclick='enviarMensaje(\""+response.data[i].id+"\",\""+response.data[i].name+"\")'/>";
                        }
                    });
                }
         
                function escribirMuro(){
                    var mensaje=prompt("What is your message?");
                    FB.api('/me/feed', 'post', { message: mensaje }, function(response) {
                        if (!response || response.error) {
                            alert('A problem ocurred');
                        } else {
                            alert('The message was sent to your wall');
                        }
                 
                    });
                }
             
                function enviarMensaje(id,name){
                    var mensaje=prompt("Enter the message to "+name);
                    FB.api('/'+id+'/feed', 'POST', { message: mensaje }, function(response) {
                        if (!response || response.error) {
                            alert('A problem ocurred');
                        } else {
                            alert('The message was sent to '+name);
                        }
                    });
                 
                }
             
            </script>
        </div>
    <fb:login-button autologoutlink="true" scope="email,status_update,publish_stream,sms"></fb:login-button><br/>
    <input type="button" value="Post on your wall" onclick="escribirMuro()"/>
    <input type="button" value="Get Friends" onclick="obtenerAmigos()"><br/>
    <div id="fb-root">
        <div id="imagenes">

        </div>
    </div>
</body>
</html>

Finally replace the APP ID in XXXXXXXXXXXXXXX





and test the application.

No hay comentarios:

Publicar un comentario