如何使用Facebook4j登录Facebook [英] How to login to facebook using Facebook4j

查看:622
本文介绍了如何使用Facebook4j登录Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在使用以下代码:

  public static配置createConfiguration(){
ConfigurationBuilder confBuilder = new ConfigurationBuilder();

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

} catch(FacebookException e){
System.err.println(创建访问令牌时出错+ e.getLocalizedMessage());
}
facebookClient.setOAuthAccessToken(accessToken);
//导致错误说{必须使用活动访问令牌来查询有关当前用户的信息}
facebookClient.postStatusMessage(来自Facebook4J的Hello World);
}

如何传递用户名密码用户?

解决方案

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

  confBuilder.setOAuthAppId(你的APPID在这里); 
confBuilder.setOAuthAppSecret(你的APPSECRET这里);

要获得AccessToken,您必须按照 here


I am developing a photo viewer, which is a java desktop application and to integrate it with facebook I am using the Facebook4J library. 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?

解决方案

You should retrieve an OAuthAppId from facebook. To do this, create an App on Facebook Developers and click "Create new app". Fill in the form, then copy paste the AppId and Appsecret from the page that appears in your code. 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

这篇关于如何使用Facebook4j登录Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆