简体   繁体   English

如何使用Facebook4j登录Facebook

[英]How to login to facebook using Facebook4j

I am developing a photo viewer, which is a java desktop application and to integrate it with facebook I am using the Facebook4J library. 我正在开发一个照片查看器,这是一个Java桌面应用程序,并将其与Facebook集成我正在使用Facebook4J库。 But I am clueless as to where to provide the username and password for the user.... 但我无法在哪里为用户提供用户名和密码....

I am using the following code : 我使用以下代码:

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."); 
}

How to pass the username and password the user? 如何传递username的用户usernamepassword

You should retrieve an OAuthAppId from facebook. 您应该从Facebook检索OAuthAppId。 To do this, create an App on Facebook Developers and click "Create new app". 为此,请在Facebook Developers上创建一个应用程序,然后单击“创建新应用程序”。 Fill in the form, then copy paste the AppId and Appsecret from the page that appears in your code. 填写表单,然后从代码中显示的页面复制粘贴AppId和Appsecret。 Example: 例:

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

To get the AccessToken, you have to build a login flow as described here 要获取AccessToken,您必须按照此处所述构建登录流程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM