2011-02-03 85 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(); 
    } 
} 

所以你所要做的就是找出你的應用程序需要具備哪些權限。請注意,用戶可以選擇「第二次對話」權限,因此不能保證您實際上擁有這些權限。

相關問題