2011-02-03 84 views
5

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

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(); 
     } 
    } 
+0

对于初学者,请转到http://restfb.com/和OAuth 2.0。 – 2012-03-14 10:36:52

+0

理查德甚至使用OAuth 2.0我还没有找到从Java桌面应用获取OAuth 2.0令牌的方法。所以你的评论并没有真正帮助回答你如何从桌面Java获得access_token的问题? – haskovec 2012-04-26 16:55:05

回答

0

首先,你需要得到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 &范围=电子邮件,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 &代码= THE_CODE_FROM_ABOVE
  3. 精确的和的access_token使用来自restfb FacebookClient使API请求。
0

AFAIK,现在还没有办法通过“桌面应用程序”以直接的方式连接到Facebook。您可以使用apache http客户端库来模拟浏览器并完成它。但是总是不能保证工作。我也一直试图在某些时候与一些图书馆一起工作,但它们看起来很糟糕。

0

我已经取得了一些成功。我的方法是使用嵌入式浏览器向用户显示身份验证。 Facebook处理身份验证并使用访问令牌将您重定向到“登录成功”页面,并将过期时间作为GET数据添加到URL上。下面的大部分代码用于创建和显示使用 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(); 
    } 
} 

所以你所要做的就是找出你的应用程序需要具备哪些权限。请注意,用户可以选择“第二次对话”权限,因此不能保证您实际上拥有这些权限。

相关问题