我需要从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/
简单总结一下:
FacebookClient来自restfb 的API 发出请求。