CPE5009 – Tutorial 1

braintreesmileSoftware and s/w Development

Aug 15, 2012 (5 years and 3 months ago)

298 views

FIT5030



Tutorial
11


Accessing and Consuming the Facebook Web API

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 document
ed
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
cons
tructed 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.


This tutorial is based on the exercises provided at:

http://www.netbeans.org/kb/docs/websvc/facebook.html



Creating the Project

The IDE lets you insert SaaS operations into servlets, existing RESTful services, JSP pages, or plain Java
objects (POJOs). I
n 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 t
he 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 Pro
perties from the context window. The
Project Properties dialog opens.

5.

Open the Run category and unselect Deploy On Save. Click OK.


6.

I
n the Services window, expand the Web Services node. Expand the Facebook node and
subnodes until you find 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 inse
rts the friends_get
operation into the body of index.jsp, which now looks like this:

8.

<body>

9.


<h1>JSP Page</h1>

10.


11.


<%@ page import="org.netbeans.saas.*, org.netbeans.saas.facebook.*"
%>

12.


<%

13.


try {

14.


String format = null;

15.


String flid =
null;

16.


17.


RestResponse result =
FacebookSocialNetworkingService.friendsGet(request, response, format,
flid);

18.


facebook.socialnetworkingservice.facebookresponse.FriendsGetResponse
resultObj =
result.getDataAsObject(facebook.socialnetworkingservi
ce.facebookresponse.
FriendsGetResponse.class);


19.


//TODO
-

Uncomment the print Statement below to print result.


//out.println("The SaasService returned:
"+result.getDataAsString());


20.


} catch (Exception ex) {


21.


ex.printStackTrace();


22.


}

23.


%>

</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 ac
cess 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.


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 st
ructure is
shown in the Projects window.


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

fol
der:

<servlet>


<servlet
-
name>FacebookSocialNetworkingServiceLogin</servlet
-
name>


<servlet
-
class>org.netbeans.saas.facebook.FacebookSocialNetworkingServiceLogin</se
rvlet
-
class>


<load
-
on
-
startup>1</load
-
on
-
startup>

</servlet>

<servlet>


<serv
let
-
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>Fa
cebookSocialNetworkingServiceLogin</servlet
-
name>


<url
-
pattern>/FacebookSocialNetworkingServiceLogin</url
-
pattern>

</servlet
-
mapping>

<servlet
-
mapping>


<servlet
-
name>FacebookSocialNetworkingServiceCallback</servlet
-
name>


<url
-
pattern>/FacebookS
ocialNetworkingServiceCallback</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.propert
ies
.

To register your project:

1.

Open a browser window and log into Facebook.

2.

Go to
http://www.facebook.com/de
velopers/
. 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 i
n 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
t
he 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(re
quest,
response, format, flid);

URLs for all user login
-
authenticated services are constructed in
this way.


8.

Leave all other fields with def
ault 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
or
g.netbeans.saas.facebook
. Copy the API key and the secret key from the browser to this
file. Save the 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 chang
es:
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 fo
r 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.