简体   繁体   中英

Fetching facebook friends by using facebook java api

My task is something like this:-

1) Make a sucessful login from my app and retrive the name of friends of user 2)Display all friends on a web page(html), each friend have a correspoing cheakboxes.(list view) 3)We should be able tp post a messege on selected friends 4)A submit/sent wall button.

So i decided to write an sevlet to make a request(foe login) and retreive friends list, as follows

public class Servletfb extends HttpServlet {
private static final long serialVersionUID = 1L;
private String API_KEY="1c36b59974ca90e4cefaa1f5c8e18edf";
private String SECRET_KEY="77f2d4df446ac8ae8b35b008da3d6070";

/**
 * Default constructor. 
 */
public Servletfb() {
    // TODO Auto-generated constructor stub
}

/**
 * @see Servlet#init(ServletConfig)
 */
public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub

      IFacebookRestClient client = new FacebookXmlRestClient(API_KEY,
                    SECRET_KEY);

            try {
                String token = client.auth_createToken();
                String url = "http://www.facebook.com/login.php?api_key=" + API_KEY
                        + "&v=1.0" + "&auth_token=" + token;
                System.out.println(url);
                Runtime.getRuntime().exec("explorer \"" + url + "\""); 

                System.out.println("Use browser to login then press return");
                System.in.read();

                String session = client.auth_getSession(token);
                System.out.println("Session key is " + session);

                client.friends_get();

            } catch (FacebookException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

}

but i got a exception something like

javax.servlet.ServletException: Error instantiating servlet class com.vibhor.Servletfb
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

I am using Tomcat Server, and face book java 3.0.2 api

as i mentioned my requirement, what would be best approach to achive this, since i am stuck on very intial phase?? any help would be appriciated...

{EDIT} I put the code into doPost() and the stack trace is:-

java.lang.NoClassDefFoundError: com/google/code/facebookapi/FacebookException
at java.lang.Class.getDeclaredConstructors0(Native Method)
na.at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:255)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4356)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.ClassNotFoundException:      com.google.code.facebookapi.FacebookException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
at org.apache.cataliloader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)

[EDIT]

OK... it seems that it doesn't working, so i switched to facebook rest api, and write something like this:

public static void main(String[] args)
{

    FacebookClient facebookClient = new DefaultFacebookClient(args[0]);
User user = facebookClient.fetchObject("me", User.class);
System.out.println("User name: " + user.getName());
Connection<User> myFriends = facebookClient.fetchConnection("me/friends",    User.class);
System.out.println("Count of my friends: " + myFriends.getData().size());
              System.out.println(myFriends.toString());
}

and my output is the list of my friends in json form:

INFO: Facebook responded with HTTP status code 200 and response body: {"data":  [{"name":"Wasim Sama","id":"550332915"},{"name":"Ujjwal Mishra","id":"599310869"},{"name":"Rj Priyanka","id":"764454678"},{"name":"Paras Arora","id":"778875857"},{"name":"Harshit Makkar","id":"1012133181"},{"name":"Sudhanshu Shukla","id":"1019487229"},{"name":"Vikas Jain","id":"1023819462"},{"name":"Vijay Boregowda","id":"1039516000"},{"name":"Tulika Gera","id":"1097057138"},{"name":"Deepak Kumar R","id":"1132446570"},{"name":"Nitin Goyal","id":"1247507205"},{"name":"Romil Choudaha","id":"1447479935"},{"name":"Saumil Gupta","id":"1501336418"},{"name":"Vebhav Gupta","id":"1507248199"},{"name":"Ashish Vimal","id":"1508694554"},{"name":"Utkarsh Singh","id":"1574203311"},{"name":"Varun Kumar","id":"1623342038"},{"name":"Pradeep Kumar Krishnia","id":"1632769647"},{"name":"Girish Grover","id":"1647181709"},{"name":"Gaurav Gopalia","id":"1661547047"},{"name":"Rajendra Kasana","id":"1664287122"},{"name":"Avdhesh Dubey","id":"1794517002"},{"name":"Shivendra Mathur","id":"1835574934"},{"name":"Vasu Jain","id":"100000009957578"},{"name":"Punit Agrawal","id":"100000118068889"},{"name":"Ramakant Jaiman","id":"100000155434269"},{"name":"Ankit Sharma","id":"100000193433562"},{"name":"Anuj Jain","id":"100000489571784"},{"name":"Camille Ali","id":"100000603236882"},{"name":"Ankur Sharma","id":"100000702407825"},{"name":"Ashutosh Kumar","id":"100000705998842"},{"name":"Prarit Gupta","id":"100000747266879"},{"name":"Amit Gupta","id":"100000781280458"},{"name":"Govind Sharma","id":"100000819234210"},{"name":"Vijay Saini","id":"100000872004792"},{"name":"Rahul Kumar","id":"100001055753094"},{"name":"Krishan Agarwal","id":"100001091775590"},{"name":"Vaibhav Singh","id":"100001147539787"},{"name":"Arvind Kumar Singh","id":"100001492873242"},{"name":"Rajendra Gupta","id":"100001502173655"},{"name":"Ajay Kumar Tiwari","id":"100001520262873"},{"name":"Alok Rai","id":"100001561421479"}]}

how to retrieve the names out of json object and display them on html page, since i have no experience with working json, it seems bit tricky to me...any help??

Though the question is very old. I am writing here for users who are landing here for a solution to fetch Facebook friends using Java.

If you making a new FB App, Facebook new API Version 2.0 do not allow apps to get all Facebook friends with default access right ie public_profile. In version 1, it was possible. Now Facebook allows to fetch only friends who are using the app already, more precisely who have already logged-in with Facebook Login dialog on you website. For that, you need to specify user_friends permission in link that opens FB Login dialog box. (There might exist some extended permission to fetch all friends.)

To see how to fetch only those FB friends who are using your app using Java/Servlets, here is an article that explains it: https://www.bitspedia.com/2012/01/how-to-get-facebook-friends-list-in.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM