Java 如何使用 Facebook4j 登录 Facebook
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20732282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to login to facebook using Facebook4j
提问by Tamojit Chatterjee
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....
我正在开发一个照片查看器,它是一个 Java 桌面应用程序,并将它与 Facebook 集成,我正在使用该Facebook4J
库。但我不知道在哪里为用户提供用户名和密码......
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
和password
用户?
回答by k1ps
You should retrieve an OAuthAppId from facebook. To do this, create an App on Facebook Developersand click "Create new app". Fill in the form, then copy paste the AppId and Appsecret from the page that appears in your code. Example:
您应该从 facebook 检索 OAuthAppId。为此,请在Facebook Developers上创建一个应用程序,然后单击“创建新应用程序”。填写表单,然后从代码中显示的页面中复制粘贴 AppId 和 Appsecret。例子:
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,您必须按照此处所述构建登录流程