2013-12-22 36 views
3

我正在開發一個照片查看器,這是一個Java桌面應用程序,並將其與Facebook集成我正在使用Facebook4J庫。 但我無能,何處爲用戶提供的用戶名和密碼....如何使用Facebook4j登錄Facebook

我使用下面的代碼:

public static Configuration createConfiguration() { 
    ConfigurationBuilder confBuilder = new ConfigurationBuilder(); 

    confBuilder.setDebugEnabled(true); 
    confBuilder.setOAuthAppId("************"); 
    confBuilder.setOAuthAppSecret("***************"); 
    confBuilder.setUseSSL(true); 
    confBuilder.setJSONStoreEnabled(true); 
    Configuration configuration = confBuilder.build(); 
    return configuration; 
} 
public static void main(String[] argv) throws FacebookException { 
    Configuration configuration = createConfiguration(); 
    FacebookFactory facebookFactory = new FacebookFactory(configuration); 
    Facebook facebookClient = facebookFactory.getInstance(); 
    AccessToken accessToken = null; 
    try{ 
     OAuthSupport oAuthSupport = new OAuthAuthorization(configuration); 
     accessToken = oAuthSupport.getOAuthAppAccessToken(); 

    }catch (FacebookException e) { 
     System.err.println("Error while creating access token " + e.getLocalizedMessage()); 
    } 
    facebookClient.setOAuthAccessToken(accessToken); 
//results in an error says {An active access token must be used to query information about the current user} 
    facebookClient.postStatusMessage("Hello World from Facebook4J."); 
} 

如何通過usernamepassword用戶?

+0

嗨我可以知道它是如何解決的手段如何提供用於登錄 – RamBen

+0

憑據,您可以使用所提供的瀏覽器javafx並從那裏獲取用戶憑證。並且還從瀏覽器短時間重定向到的臨時鏈接中獲取訪問令牌。 看看我的git hub項目已經這樣做了... https://github.com/tamojit9/java-facebook-api –

+0

謝謝Tamojit Chatterjee,只是我在這裏檢查了你的github代碼我需要登錄任何登錄表單意味着自動登錄/憑證供應 – RamBen

回答

4

您應該從facebook中檢索OAuthAppId。爲此,請在Facebook Developers上創建應用程序,然後點擊「創建新應用程序」。填寫表單,然後從代碼中出現的頁面複製粘貼AppId和Appsecret。例如:

confBuilder.setOAuthAppId("YOUR APPID HERE"); 
confBuilder.setOAuthAppSecret("YOUR APPSECRET HERE"); 

要獲得的accessToken,你必須建立一個登錄流程描述here

+0

我已經構建了一個應用程序,並具有應用程序id和appsecret,但沒有透露它在這裏出於顯而易見的原因。我在設計登錄流程時出現問題 –

+0

您必須發送HTTPS請求到facebook.com: GET https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials(請留下client_credentials,因爲它是)。 – k1ps

+0

嗨k1ps使用上述鏈接,我們將得到應用程序accesstoken,如果我們需要用戶accesstoken沒有明確的登錄,我們可以得到。意味着需要以編程方式獲取用戶acesstoken(可以自動使用fb登錄) – RamBen