简体   繁体   English

用facebook4j在页面上发布

[英]Posting on Page with facebook4j

Is there a way of how to post on facebook page wall? 有没有一种方法可以在Facebook页面墙上发布? From tutorials is just showing how to get info about page. 从教程中只是显示了如何获取有关页面的信息。 I wan to be able to post on public page(not my own but One that customer has admin rights). 我希望能够在公共页面上发布(不是我自己的页面,而是该客户具有管理员权限的页面)。

I also tried using app solution and I succesfully got OAuthAppAccessToken, but it's not enough. 我也尝试使用应用程序解决方案,但成功获得了OAuthAppAccessToken,但这还不够。

An active access token must be used to query information about the current user.

Is there some tutorial? 有教程吗? because most people just want to get like and comments from pages. 因为大多数人只想从页面中获得喜欢和评论。

Option 1 (get token via tools) 选项1(通过工具获取令牌)

This option requires manual entering and copying generated tokens via graph api tools. 此选项需要通过图形API工具手动输入和复制生成的令牌。 I am not going to cover this option much because these two links obtaining facebook page access token the 4 step program and Post to Facebook Page wall using RestFB api are covering it pretty well. 我将不讨论这个选项,因为这两个链接获得了Facebook步骤访问令牌的4步程序使用RestFB api发布到Facebook页面的墙,都很好地覆盖了该选项。


Option 2 (one button solution) 选项2(一键式解决方案)

Now this is pretty automated solution which (if you are like me) you want. 现在,这是一个非常自动化的解决方案,您需要(如果您像我一样)。 Since I couldn't tell my client: "Go here, copy this, gimme this and stuff...". 由于我无法告诉我的客户:“去这里,复制此内容,给我这个东西……”。 I needed to do most user friendly solution. 我需要做大多数用户友好的解决方案。 In the end I implemented FB login button and simple ajax call that will get long lived page access token. 最后,我实现了FB登录按钮和简单的ajax调用,这些调用将获得长寿命的页面访问令牌。 With this token, our app can post on his page automatically when some event occurs. 使用此令牌,当发生某些事件时,我们的应用程序可以自动在其页面上发布。 Using the obtaining facebook page access token the 4 step program tutorial here is the solution: 使用获取Facebook页面访问令牌的4步程序教程,这里是解决方案:

  1. Make your application https://developers.facebook.com/apps/ (you may need to add website platform and make it live). 使您的应用程序为https://developers.facebook.com/apps/ (您可能需要添加网站平台并使之运行)。
  2. In dashboard retrieve app id and app secret. 在仪表板中检索应用程序ID和应用程序密钥。
  3. Implement log in button on your website. 在您的网站上实施登录按钮。 A great info about this can be found here fb login for web . 有关此方面的重要信息,请参见fb Web登录 Code snippet there is all you need, just replace app id with your app id. 您需要的代码片段只是用您的应用程序ID替换应用程序ID。
  4. Add scopes in login-button so we can obtain pages as well have permissions to do publish actions. 在登录按钮中添加范围,这样我们就可以获得页面以及具有执行发布操作的权限。

<fb:login-button scope="public_profile,email,manage_pages,publish_actions" onlogin="checkLoginState();"> </fb:login-button>

  1. In login button you can see function that is called every time when login is invoked. 在登录按钮中,您可以看到每次调用登录时都会调用的函数。 In this function we can get response from FB with tokens and info we need about our user (in this case it's really just token we need). 在此功能中,我们可以从FB获得带有令牌和我们所需的关于用户的信息的响应(在这种情况下,它实际上只是我们需要的令牌)。 Following javascript code sends user token (short lived) via ajax, to our server. 以下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' ); }

  2. The next step is server side one. 下一步是服务器端。 At the moment we receive token, we need to convert it to long lived one. 目前我们收到令牌,我们需要将其转换为寿命长的令牌。

      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"); 
  3. Now the last step we need to obtain access token for page we want to post at. 现在,最后一步,我们需要获取要发布到的页面的访问令牌。 From this point on we can use facebook4j. 从这一点开始,我们可以使用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(); } } 
  4. PROFIT: with this token we can post on desired page: 利润:使用此令牌,我们可以在所需的页面上发布:

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

Side note : This solution is not meant to be used as a page spammer. 旁注 :此解决方案不能用作垃圾邮件发送者。 User needs to be informed what events will trigger posting on his page. 需要告知用户哪些事件将触发在其页面上的发布。 If user wants to reduce/remove permissions he can do in FB settings. 如果用户要减少/删除权限,可以在FB设置中进行。

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

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