使用 facebook4j 在页面上发布
Posting on Page with facebook4j
有没有办法在facebook页面墙上post?从教程中只是展示如何获取有关页面的信息。我希望能够在 public 页面上 post(不是我自己的,而是客户拥有管理员权限的页面)。
我也试过使用app解决方案,成功获得了OAuthAppAccessToken,但是还是不够。
An active access token must be used to query information about the current user.
有教程吗?因为大多数人只想从页面上获得点赞和评论。
方案一(通过工具获取令牌)
此选项需要通过图形 api 工具手动输入和复制生成的标记。我不会过多介绍这个选项,因为这两个链接
obtaining facebook page access token the 4 step program and Post to Facebook Page wall using RestFB api 覆盖得很好。
选项 2(一键解决方案)
现在这是一个非常自动化的解决方案(如果您像我一样)。因为我不能告诉我的客户:"Go here, copy this, gimme this and stuff..."。我需要做最用户友好的解决方案。最后,我实现了 FB 登录按钮和简单的 ajax 调用,它将获得长期存在的页面访问令牌。有了这个令牌,我们的应用程序可以在某些事件发生时自动 post 在他的页面上。使用这里的obtaining facebook page access token the 4 step program教程是解决方案:
- 制作您的应用程序https://developers.facebook.com/apps/(您可能需要添加网站平台并使其上线)。
- 在仪表板中检索应用程序 ID 和应用程序密码。
- 在您的网站上实现登录按钮。可以在此处 fb login for web 找到关于此的重要信息。代码片段有你需要的一切,只需将应用程序 ID 替换为你的应用程序 ID。
- 在登录按钮中添加范围,以便我们可以获得页面以及执行发布操作的权限。
<fb:login-button scope="public_profile,email,manage_pages,publish_actions" onlogin="checkLoginState();">
</fb:login-button>
在登录按钮中,您可以看到每次调用登录时都会调用的函数。在这个函数中,我们可以从 FB 获得响应,其中包含我们需要的有关用户的令牌和信息(在这种情况下,它实际上只是我们需要的令牌)。以下 javascript 代码通过 ajax 将用户令牌(短期)发送到我们的服务器。
function checkLoginState() {
FB.getLoginStatus(function (response) {
statusChangeCallback(response);
});
}
function statusChangeCallback(response) {
if (response.status === 'connected') {
getLongLivedToken(response.authResponse.accessToken);
}
}
function getLongLivedToken(access) {
var data = {
${fbParam}: acces
};
$.post(
'${fbUrl}',
data,
function (INFO) {
console.log("done");
},
'text'
);
}
下一步是服务器端。在我们收到令牌的那一刻,我们需要将其转换为长期存在的令牌。
String url = "https://graph.facebook.com/oauth/access_token";
String charset = "UTF-8";
String grandType = "fb_exchange_token";
String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&fb_exchange_token=%s",
URLEncoder.encode(grandType, charset),
URLEncoder.encode(Constants.FACEBOOK_APP_ID, charset),
URLEncoder.encode(Constants.FACEBOOK_APP_SECRET, charset),
URLEncoder.encode(shortToken, charset));
HttpsURLConnection con = (HttpsURLConnection) new URL(url + "?" + query).openConnection();
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;
String result = "";
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
result += inputLine;
}
in.close();
String[] params = result.split("&");
Map<String, String> map = new HashMap<String, String>();
for (String param : params) {
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
}
String longToken=map.get("access_token");
现在是最后一步,我们需要为我们想要 post 的页面获取访问令牌。从此我们就可以使用facebook4j了。
Facebook facebook = new FacebookFactory().getInstance();
facebook.setOAuthAppId(Constants.FACEBOOK_APP_ID, Constants.FACEBOOK_APP_SECRET);
facebook.setOAuthAccessToken(new AccessToken(longToken));
try {
String pageToken = null;
for (Account a : facebook.getAccounts()) {
if (a.getName().toLowerCase().contains("nameOfPage")) {
pageToken = a.getAccessToken();
}
}
利润:使用此令牌,我们可以 post 在所需页面上:
PostUpdate post = new PostUpdate(new URL("http://priklad.sk"))
.picture(new URL("http://priklad.sk/obrazcok/testik.png"))
.name("priklad")
.caption("priklad")
.message("priklad")
.description("priklad");
try {
if (pageToken != null) {
facebook.setOAuthAccessToken(new AccessToken(id));
facebook.postFeed(post);
Input.addInfoAnnotation(req, "sysAdminTools.annotation.fb.ok");
}
} catch (FacebookException ex) {
Logger.getLogger(EditAdPreviewServlet.class.getName()).log(Level.SEVERE, null, ex);
}
旁注:此解决方案不适合用作页面垃圾邮件发送者。用户需要被告知哪些事件将在他的页面上触发 posting。如果用户想要 reduce/remove 权限,他可以在 FB 设置中执行。
有没有办法在facebook页面墙上post?从教程中只是展示如何获取有关页面的信息。我希望能够在 public 页面上 post(不是我自己的,而是客户拥有管理员权限的页面)。
我也试过使用app解决方案,成功获得了OAuthAppAccessToken,但是还是不够。
An active access token must be used to query information about the current user.
有教程吗?因为大多数人只想从页面上获得点赞和评论。
方案一(通过工具获取令牌)
此选项需要通过图形 api 工具手动输入和复制生成的标记。我不会过多介绍这个选项,因为这两个链接 obtaining facebook page access token the 4 step program and Post to Facebook Page wall using RestFB api 覆盖得很好。
选项 2(一键解决方案)
现在这是一个非常自动化的解决方案(如果您像我一样)。因为我不能告诉我的客户:"Go here, copy this, gimme this and stuff..."。我需要做最用户友好的解决方案。最后,我实现了 FB 登录按钮和简单的 ajax 调用,它将获得长期存在的页面访问令牌。有了这个令牌,我们的应用程序可以在某些事件发生时自动 post 在他的页面上。使用这里的obtaining facebook page access token the 4 step program教程是解决方案:
- 制作您的应用程序https://developers.facebook.com/apps/(您可能需要添加网站平台并使其上线)。
- 在仪表板中检索应用程序 ID 和应用程序密码。
- 在您的网站上实现登录按钮。可以在此处 fb login for web 找到关于此的重要信息。代码片段有你需要的一切,只需将应用程序 ID 替换为你的应用程序 ID。
- 在登录按钮中添加范围,以便我们可以获得页面以及执行发布操作的权限。
<fb:login-button scope="public_profile,email,manage_pages,publish_actions" onlogin="checkLoginState();">
</fb:login-button>
在登录按钮中,您可以看到每次调用登录时都会调用的函数。在这个函数中,我们可以从 FB 获得响应,其中包含我们需要的有关用户的令牌和信息(在这种情况下,它实际上只是我们需要的令牌)。以下 javascript 代码通过 ajax 将用户令牌(短期)发送到我们的服务器。
function checkLoginState() { FB.getLoginStatus(function (response) { statusChangeCallback(response); }); } function statusChangeCallback(response) { if (response.status === 'connected') { getLongLivedToken(response.authResponse.accessToken); } } function getLongLivedToken(access) { var data = { ${fbParam}: acces }; $.post( '${fbUrl}', data, function (INFO) { console.log("done"); }, 'text' ); }
下一步是服务器端。在我们收到令牌的那一刻,我们需要将其转换为长期存在的令牌。
String url = "https://graph.facebook.com/oauth/access_token"; String charset = "UTF-8"; String grandType = "fb_exchange_token"; String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&fb_exchange_token=%s", URLEncoder.encode(grandType, charset), URLEncoder.encode(Constants.FACEBOOK_APP_ID, charset), URLEncoder.encode(Constants.FACEBOOK_APP_SECRET, charset), URLEncoder.encode(shortToken, charset)); HttpsURLConnection con = (HttpsURLConnection) new URL(url + "?" + query).openConnection(); InputStream ins = con.getInputStream(); InputStreamReader isr = new InputStreamReader(ins); BufferedReader in = new BufferedReader(isr); String inputLine; String result = ""; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); result += inputLine; } in.close(); String[] params = result.split("&"); Map<String, String> map = new HashMap<String, String>(); for (String param : params) { String name = param.split("=")[0]; String value = param.split("=")[1]; map.put(name, value); } String longToken=map.get("access_token");
现在是最后一步,我们需要为我们想要 post 的页面获取访问令牌。从此我们就可以使用facebook4j了。
Facebook facebook = new FacebookFactory().getInstance(); facebook.setOAuthAppId(Constants.FACEBOOK_APP_ID, Constants.FACEBOOK_APP_SECRET); facebook.setOAuthAccessToken(new AccessToken(longToken)); try { String pageToken = null; for (Account a : facebook.getAccounts()) { if (a.getName().toLowerCase().contains("nameOfPage")) { pageToken = a.getAccessToken(); } }
利润:使用此令牌,我们可以 post 在所需页面上:
PostUpdate post = new PostUpdate(new URL("http://priklad.sk")) .picture(new URL("http://priklad.sk/obrazcok/testik.png")) .name("priklad") .caption("priklad") .message("priklad") .description("priklad"); try { if (pageToken != null) { facebook.setOAuthAccessToken(new AccessToken(id)); facebook.postFeed(post); Input.addInfoAnnotation(req, "sysAdminTools.annotation.fb.ok"); } } catch (FacebookException ex) { Logger.getLogger(EditAdPreviewServlet.class.getName()).log(Level.SEVERE, null, ex); }
旁注:此解决方案不适合用作页面垃圾邮件发送者。用户需要被告知哪些事件将在他的页面上触发 posting。如果用户想要 reduce/remove 权限,他可以在 FB 设置中执行。