如何将facebook与java桌面应用程序连接起来

pra*_*eep 5 java facebook

我需要从java桌面应用程序连接和验证用户,我已经尝试使用facebookjsonclient和facebookRestclient的facebook-java-api但无法获取会话密钥.Facebook是否有任何变化,因为我们无法连接,或者是否有其他最好的java api或示例如何连接.我的代码是

private static void getUserID(String email, String password){
        String session = null;
        try {

            HttpClient http = new HttpClient();

            http.getHostConfiguration().setHost("www.facebook.com");
            String api_key = "key";
            String secret = "sec";
            FacebookJaxbRestClient client = new FacebookJaxbRestClient(api_key, secret);
                System.out.println("====>"+client.isDesktop());

            String token = client.auth_createToken();
            System.out.println(" :::::::"+token);
            System.out.println(" :::::::::: "+token);
            PostMethod post = new PostMethod("/login.php?");

            post.addParameter("api_key", api_key);


            post.addParameter("email", email);
            post.addParameter("pass", password);


            int postStatus = http.executeMethod(post);
                System.out.println("url : " + post.getURI());
            System.out.println("Response : " + postStatus);
            for (Header h : post.getResponseHeaders()) {
                System.out.println(h);
            }
            session = client.auth_getSession(token); // Here I am getting error
            System.out.println("Session string: " + session);
            long userid = client.users_getLoggedInUser();
            //System.out.println("User Id is : " + userid);*/
        } catch (FacebookException fe) {

            fe.printStackTrace();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Art*_*Art -1

首先,您需要获取access_token,然后我建议使用restfb库。为了获得令牌,我建议阅读以下内容:https://developers.facebook.com/docs/authentication/

简单总结一下:

  1. HTTP GET: https: //www.facebook.com/dialog/oauth?client_id = YOUR_APP_ID&redirect_uri = YOUR_URL&scope =电子邮件,read_stream&response_type =令牌
  2. 使用从上面获得的代码,然后执行: https: //graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL& client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
  3. 精确获取 access_token 并使用FacebookClient来自restfb 的API 发出请求。

  • 他试图从 Java 桌面应用程序执行此操作,这并不意味着他有一个要重定向到的 URL(如果它是 Java 应用程序),所以基本上您还没有回答有关如何从桌面应用程序获取令牌的任何问题,你只要说得到它。 (7认同)