如何使用Facebook4j登录Facebook

时间:2013-12-22 17:53:00

标签: java facebook facebook-graph-api oauth

我正在开发一个照片查看器,这是一个java桌面应用程序,并将其与facebook集成我正在使用Facebook4J库。 但我无法在哪里为用户提供用户名和密码....

我使用以下代码:

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

1 个答案:

答案 0 :(得分:4)

您应该从Facebook检索OAuthAppId。为此,请在Facebook Developers上创建一个应用,然后点击“创建新应用”。填写表单,然后从代码中显示的页面复制粘贴AppId和Appsecret。例如:

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

要获取AccessToken,您必须按照here

所述构建登录流程