corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

SaaS: Facebook

NetBeans IDE provides a Web Services Manager that supports Software as a Service (SaaS) applications. SaaS refers to a software application delivery model where a software vendor develops a web-native software application and hosts and operates the application for use by its customers over the Internet. SaaS is an increasingly popular model for providing software functionality as it is economical in terms of both cost and customer hardware resources. The IDE now makes it easy for Java developers to access all the popular SaaS services on the web.

The Facebook Platform is a standards-based web service with methods for accessing and contributing Facebook data. The Facebook Platform is documented on the Facebook Developers Wiki.

Facebook secures web service communication with the User Login mechanism. The application developer needs to register the application with Facebook to get an API key and a secret key. Every method call to Facebook needs to be signed using the secret key. During the login process, the application first calls Facebook to request a token using the API key. Next, the user is redirected to a login URL constructed from the API key and the token. This URL takes the user to a login page on Facebook. After the user logs into Facebook and authorizes the application to call Facebook on his/her behalf, the application sends back a session key and a session secret key. From them on, all API calls must pass the session key and be signed using the session secret key.

In this tutorial, you use the simple friends_get operation inserted into a JSP file to return a list of the numeric IDs of all a user's Facebook friends. The tutorial focuses on showing you how to set up the complicated Facebook User Login authentication. By analogy this should help you with other User Login services, such as Flickr's.

Contents

Content on this page applies to NetBeans IDE 6.5

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Web & Java EE download bundle 6.1, including latest patch
Java download bundle 6.5
Java Development Kit (JDK) version 6 or
version 5
Java EE-compliant web or application server Tomcat web server 6.0 and/or GlassFish application server v2
Important: If you use Tomcat with this Tutorial, see
the NetBeans Wiki on Tomcat with REST

Creating the Project

The IDE lets you insert SaaS operations into servlets, existing RESTful services, JSP pages, or plain Java objects (POJOs). In this tutorial, you create a web application project and add the service into the project's default JSP page.

To create the project:

  1. Choose File > New Project. Under Categories, select Java Web. Under Projects, select Web Application and click Next.
  2. In the Project Name field, type MyFriendsSaas.
  3. Select the GlassFish or Tomcat server and Java EE 5. Click through the remaining options and click Finish. The project's index.jsp page opens in the IDE.
  4. Right-click the MyFriendsSaas project node and select Properties from the context window. The Project Properties dialog opens.
  5. Open the Run category and unselect Deploy On Save. Click OK.
    Project Properties with Deploy on Save unselected
  6. In the Services window, expand the Web Services node. Expand the Facebook node and subnodes until you find the friends_get operation.
    Services window with expanded Facebook web services, showing the friends_get operation
  7. Left-click the friends_get operation. Hold the left mouse button down and drag the operation into the body of index.jsp. The Customize GET SaaS dialog opens, in which you can set initial parameter values. Leave the default values and click OK. The IDE inserts the friends_get operation into the body of index.jsp, which now looks like this:
    <body>
      <h1>JSP Page</h1>
    
      <%@ page import="org.netbeans.saas.*, org.netbeans.saas.facebook.*" %>
      <%
          try {
            String format = null;
            String flid = null;
    
            RestResponse result = FacebookSocialNetworkingService.friendsGet(request, response, format, flid);
            facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse resultObj = result.getDataAsObject(facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse.class);
    //TODO - Uncomment the print Statement below to print result.
    //out.println("The SaasService returned: "+result.getDataAsString());
    } catch (Exception ex) {
    ex.printStackTrace();
    } %> </body>

    If the compiler does not recognize the FriendsGetResponse class, you need to install the latest patches to the NetBeans IDE. This patch includes support for JAXB FriendsGetResponse.

    You can use the FriendsGetResponse JAXB object to access the underlying XML document. Add an empty line after the facebook.socialnetworkingservice... line and type resultObj.. A pop-up window appears with all the methods that extend resultObj and that you can use to access the underlying XML.
    Code completion for the resultObject method

    The IDE also adds classes and the security key property file in the packages org.netbeans.saas and org.netbeans.saas.facebook. The complete project structure is shown in the Projects window.

    Projects window showing complete project structure

    Lastly, the IDE inserts the following section into web.xml, which is in the Web Pages/WEB-INF folder:

    <servlet>
        <servlet-name>FacebookSocialNetworkingServiceLogin</servlet-name>
        <servlet-class>org.netbeans.saas.facebook.FacebookSocialNetworkingServiceLogin</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>FacebookSocialNetworkingServiceCallback</servlet-name>
        <servlet-class>org.netbeans.saas.facebook.FacebookSocialNetworkingServiceCallback</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FacebookSocialNetworkingServiceLogin</servlet-name>
        <url-pattern>/FacebookSocialNetworkingServiceLogin</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>FacebookSocialNetworkingServiceCallback</servlet-name>
        <url-pattern>/FacebookSocialNetworkingServiceCallback</url-pattern>
    </servlet-mapping>

Registering the Project

Register the application with Facebook to get an API key and a secret key. You need to have a Facebook account. When you get the keys, copy them out of your browser and paste them into org.netbeans.saas.facebook.facebooksocialnetworkingserviceauthenticator.properties.

To register your project:

  1. Open a browser window and log into Facebook.
  2. Go to http://www.facebook.com/developers/. The Developer page opens.
  3. Click + Set Up New Application. The New Application page opens.
  4. In the Application Name field, type MyFriendsSaas. The Application name must always match the web app project name.
  5. Expand the Optional Fields.
  6. Type in developer and user support email addresses.
  7. Under Callback URL, type http://localhost:8080/MyFriendsSaas/FacebookSocialNetworkingServiceCallback. This URL is derived from the port number on which GlassFish is running, the project name, and the name of the class passing the result. (If you are using a server on a different port than 8080, type that port number instead.) You can see the name of this class in the index.jsp file, in the line RestResponse result = FacebookSocialNetworkingService.friendsGet(request, response, format, flid); URLs for all user login-authenticated services are constructed in this way.
    New Application page on Facebook Developer site showing name, email and Callback URL fields filled in
  8. Leave all other fields with default values. Click Submit. The browser refreshes with the MyFriendsSaas application home page. Newly generated API and secret keys are displayed.
  9. In the IDE, go to the Projects window and open facebooksocialnetworkingserviceauthenticator.properties, in org.netbeans.saas.facebook. Copy the API key and the secret key from the browser to this file. Save the file.
    Projects window showing facebook social networking service authenticator properties file

Running the Project

After you have retrieved the API and secret keys from Facebook, you can run your project.

To run the project:

  1. Open index.jsp and uncomment the print statement out.println("The SaasService returned: "+result.getDataAsString());. You can also make some cosmetic changes: change the title and h1 header from "JSP Page" to "Facebook Get Friends Service," change the text in the try block from "The SaasService returned:" to "Your friendlist IDs are:", or any other such changes you would like to try.
  2. Right-click the project's node in the Projects window. Select Run from the context menu. The IDE builds your project and deploys it to the application server.
  3. A browser window opens in which you are asked to log into Facebook. Click Log Into Facebook and a login page appears for the MyFriendsSaas project. Log in with your Facebook credentials.
  4. A warning might appear that a web page is trying to access your intranet. Allow the web page to access your intranet.
  5. A list of the numerical id's of your friends list is returned in the browser.

More Exercises

Here are a few more ideas for you to explore:

  • Redesign index.jsp to present the data in a more visually appealing way than a list of numbers.
  • Add additional Facebook operations to the project. Design index.jsp to display a table of information about the user's friends, for instance.
  • Try accessing Facebook operations from a Java Desktop Application instead of a web application. For testing purposes, you can drop the Facebook operation into the main method of a main class. You could also create a GUI and drop the operation into that class. When you register your application with Facebook, you need to specify that it is a desktop application and you do not need to specify the callback.  Also, when you run the application, a dialog comes up with a URL and the user needs to copy and paste the URL into a browser to log into Facebook.


See Also

For more information about using NetBeans IDE to develop RESTful web services, SaaS, and other Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems