如何将facebook与java桌面应用程序连接
我需要从java桌面应用程序连接和验证用户,我使用facebookjsonclient和facebookRestclient尝试了facebook java api,但无法获得会话密钥。facebook是否有任何我们无法连接的变化,或者是否有其他最好的java api或如何连接的示例。我的代码是如何将facebook与java桌面应用程序连接,java,facebook,Java,Facebook,我需要从java桌面应用程序连接和验证用户,我使用facebookjsonclient和facebookRestclient尝试了facebook java api,但无法获得会话密钥。facebook是否有任何我们无法连接的变化,或者是否有其他最好的java api或如何连接的示例。我的代码是 private static void getUserID(String email, String password){ String session = null;
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();
}
}
首先,您需要获取
access\u令牌
,然后我建议使用restfb库。为了获得令牌,我建议阅读以下内容:
一个简单的总结:
FacebookClient
发出API请求顺便说一句,目前还没有办法直接从“桌面应用程序”连接到facebook。您可以使用ApacheHTTP客户端库来模拟浏览器并完成它。但是不能保证它总是有效的。我也试着用一些库做了一段时间,但它们似乎坏了。我在这方面取得了一些成功。我的方法是使用嵌入式浏览器向用户显示身份验证。Facebook处理身份验证,并将您重定向到“登录成功”页面,访问令牌和过期时间作为GET数据固定在URL上。下面的大部分代码用于使用
org.eclipse.sw
t库创建和显示浏览器
private static final String APP_ID = "###########";
private static final String PERMISSIONS =
"COMMA SEPARATED LIST OF REQUESTED PERMISSIONS";
private String access_token;
private long expirationTimeMillis;
/**
* Implements facebook's authentication flow to obtain an access token. This
* method displays an embedded browser and defers to facebook to obtain the
* user's credentials.
* According to facebook, the request as we make it here should return a
* token that is valid for 60 days. That means this method should be called
* once every sixty days.
*/
private void authenticationFlow() {
Display display = new Display();
Shell shell = new Shell(display);
final Browser browser;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
try {
browser = new Browser(shell, SWT.NONE);
} catch (SWTError e){
System.err.println("Could not instantiate Browser: " + e.getMessage());
display.dispose();
display = null;
return;
}
browser.setJavascriptEnabled(true);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
browser.setLayoutData(data);
final ProgressBar progressBar = new ProgressBar(shell, SWT.MOZILLA);
data = new GridData();
data.horizontalAlignment = GridData.END;
progressBar.setLayoutData(data);
/* Event Handling */
browser.addProgressListener(new ProgressListener(){
public void changed(ProgressEvent event){
if(event.total == 0) return;
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}
public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.addLocationListener(new LocationListener(){
public void changed(LocationEvent e){
// Grab the token if the browser has been redirected to
// the login_success page
String s = e.location;
String token_identifier = "access_token=";
if(s.contains("https://www.facebook.com/connect/login_success.html#access_token=")){
access_token = s.substring(s.lastIndexOf(token_identifier)+token_identifier.length(),s.lastIndexOf('&'));
String expires_in = s.substring(s.lastIndexOf('=')+1);
expirationTimeMillis = System.currentTimeMillis() + (Integer.parseInt(expires_in) * 1000);
}
}
public void changing(LocationEvent e){}
});
if(display != null){
shell.open();
browser.setUrl("https://www.facebook.com/dialog/oauth?"
+ "client_id=" + APP_ID
+ "&redirect_uri=https://www.facebook.com/connect/login_success.html"
+ "&scope=" + PERMISSIONS
+ "&response_type=token");
while(!shell.isDisposed()) {
if(!display.readAndDispatch()){
display.sleep();
if(access_token != null && !access_token.isEmpty()){
try{ Thread.sleep(3000);}catch(Exception e){}
shell.dispose();
}
}
}
display.dispose();
}
}
因此,您所要做的就是确定应用程序工作需要哪些权限。请注意,用户可以从中选择“第二个对话框”权限,因此不能保证您实际拥有这些权限。对于初学者,请转到和OAuth 2.0。问题是,即使是OAuth 2.0,我也没有找到从Java桌面应用程序获取OAuth 2.0令牌的方法。因此,你的评论并不能真正帮助回答如何从桌面java获得访问令牌的问题?嗨,pradeep..你能为上述问题发布完整的代码吗?Y因为在过去的一周里,我一直在搜索facebook登录,但我仍然无法获得..因此,如果你发布完整的代码,那就太好了..有同样的问题,而且Facebook和Google+似乎都不允许老式的登录,只允许重定向式的身份验证。我认为这可能是为了防止机器人。不允许用户名+密码令牌检索在安全方面没有任何意义。他试图从Java桌面应用程序中执行此操作,但这并不意味着他有一个要重定向到的URL(如果它是Java应用程序),因此基本上你没有回答任何关于如何从桌面应用程序中获取令牌的问题,你只是说获取它。