Create a sample java facebook application
This is a spring controller class which I created to use the java facebook api. This works as is and can be dropped into a java class and jsp file. This basically brings all the users info from the api to the jsp page. If you need help about about how to setup a
spring framework application you can
click here.
This class looks like:
package facebook;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.facebook.api.FacebookException;
import com.facebook.api.FacebookXmlRestClient;
import com.facebook.api.ProfileField;
public class FaceBookController implements Controller {
public String apiKey = "ba2c5adb61c1d6d4fbc9a9fb566806e4";
public String secretKey = "c44a45272b18cbedeb145a4d1e80b719";
FacebookXmlRestClient frc;
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FacebookException {
Map
myModel = new HashMap();
HttpSession session = request.getSession();
String sessionKey = (String) session.getAttribute("facebookSession");
String token = request.getParameter("auth_token");
try {
if (sessionKey != null && sessionKey.length() > 0) {
logger.info("session is not null: "+sessionKey);
frc = new FacebookXmlRestClient(apiKey, secretKey, sessionKey);
//this.doTheThing(request, response, frc);
// put into model
int myid = frc.users_getLoggedInUser();
EnumSet fields = EnumSet.of(com.facebook.api.ProfileField.NAME, com.facebook.api.ProfileField.PIC, com.facebook.api.ProfileField.PIC_BIG, com.facebook.api.ProfileField.PIC_SMALL);
Collection users = new ArrayList();
users.add(myid);
// Get my information
Document d = frc.users_getInfo(users, fields);
String myname = d.getElementsByTagName("name").item(0).getTextContent();
String mypicture = d.getElementsByTagName("pic").item(0).getTextContent();
// Get my friends id
Document d2 = frc.friends_get();
String s = d2.toString();
NodeList userIDNodes = d2.getElementsByTagName("uid");
int fcount = userIDNodes.getLength();
Collection friends = new ArrayList();
for (int i = 0; i < fcount; i++) {
Node node = userIDNodes.item(i);
String idText = node.getTextContent();
Integer id = Integer.valueOf(idText);
friends.add(id);
}
List l = new ArrayList();
Map m = new HashMap();
Document d3 = frc.users_getInfo(friends, fields);
// Get my friends information
for (int j = 0; j < fcount; j++) {
String name2 = d3.getElementsByTagName("name").item(j).getTextContent();
String picture2 = d3.getElementsByTagName("pic").item(j).getTextContent();
String picture3 = d3.getElementsByTagName("pic_small").item(j).getTextContent();
String picture4 = d3.getElementsByTagName("pic_big").item(j).getTextContent();
MyFriend mf = new MyFriend();
logger.info("friends: ");
logger.info("friends: "+name2);
mf.setName(name2);
mf.setPictureURL(picture2);
l.add(mf);
}
myModel.put("friends", l);
myModel.put("friends2", l);
myModel.put("myname", myname);
myModel.put("mypicture", mypicture);
myModel.put("numFriends", new Integer(fcount));
//me
myModel.put("myname", myname);
} else if (token != null) {
logger.info("token is not null: "+token);
frc = new FacebookXmlRestClient(apiKey, secretKey);
session.setAttribute("facebookSession", sessionKey);
sessionKey = frc.auth_getSession(token);
session.setAttribute("facebookSession", sessionKey);
logger.info("return to facebook.htm");
return new ModelAndView("redirect:/facebook.htm?auth_token=" + token);
} else {
logger.info("redirect to login: "+apiKey);
response.sendRedirect("http://www.facebook.com/login.php?api_key=" + apiKey + "&v=1.0");
}
} catch (FacebookException fe) {
} catch (IOException ioe) {
}
return new ModelAndView("facebook", "model", myModel);
}
}
The JSP page looks like (add to < to all tags because I'm unable to post tags):
html>
head>title>facebook test/title>/head>
body>
H1>View All of Your Friends/H1>
I know this isn't very mind blowing but I was testing out using the facebook api and here's what I created!
/br>
c:forEach items="${model.friends}" var="friend">
c:out value="${friend.name}"/> /br>img src=""/>br>br>
/c:forEach>
/body>
/html>
1 Comments:
You write very well.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home