/*--------------------------------------------------------------------------+
| Copyright (c) 2006 Facebook, Inc. |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions |
| are met: |
| |
| 1. Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+---------------------------------------------------------------------------+
| For help with this library, contact api-help@facebook.com |
+--------------------------------------------------------------------------*/
package com.moochspot.util;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.facebook.api.FacebookException;
import com.facebook.api.FacebookRestClient;
/**
* Wrapper around the Facebook API to encapsulate operations on friends.
* This allows application code to deal with a couple of high-level objects,
* {@link FacebookFriend} (representing information about a
* single friend) and {@link FacebookFriends} (representing a
* collection of friends).
*
* To customize which fields are fetched from Facebook, first edit the
* {@link FacebookFriend} class to add whatever fields you need.
* Then modify this class's {@link #extractFriend(Node)
* extractFriend()} method (and optionally {@link #getFriend(String)
* getFriend()} to extract the new fields.
*/
public class FacebookApi {
public static String DEFAULT_API_URL = "http://api.facebook.com/restserver.php";
private Logger _log = Logger.getLogger(getClass().getName());
protected FacebookRestClient _client;
/**
* Constructs a new Facebook API wrapper.
*
* @param serverUrl
* URL of API server to connect to.
* @param sessionKey
* Session key as returned by facebook.auth.getSession (which
* is wrapped by this class's {@link #fetchCredentials(String)
* fetchCredentials()} method). Null if this wrapper should not
* be associated with a session, e.g. because it is being used to handle
* a new login.
*/
public FacebookApi(String serverUrl, String sessionKey) {
try {
_client = new FacebookRestClient(serverUrl, sessionKey, Constants.FACEBOOK_KEY, Constants.FACEBOOK_SHARED_SECRET);
}
catch (MalformedURLException e) {
_log.severe("Bad server URL " + serverUrl);
throw new RuntimeException(e);
}
}
/**
* Constructs a new Facebook API wrapper.
*
* @param sessionKey
* Session key as returned by facebook.auth.getSession (which
* is wrapped by this class's {@link #fetchCredentials(String)
* fetchCredentials()} method). Null if this wrapper should not
* be associated with a session, e.g. because it is being used to handle
* a new login.
*/
public FacebookApi(String sessionKey) {
this(DEFAULT_API_URL, sessionKey);
}
/**
* Fetches the user ID for a session key.
*
* @param authToken
* Authentication token as passed in on query string after redirection
* from Facebook login page.
* @return null if the session key was invalid or Facebook couldn't be
* contacted.
*/
public FacebookCredentials fetchCredentials(String authToken) {
if (authToken == null)
return null;
FacebookCredentials creds = new FacebookCredentials();
Document document = null;
try {
List