用facebook4j发布在页面上 [英] Posting on Page with facebook4j

查看:199
本文介绍了用facebook4j发布在页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法如何在Facebook页面墙上张贴?从教程中只是显示如何获取关于页面的信息。我想要能够在公共页面上发布(不是我自己的,而是一个客户拥有管理员权限)。

Is there a way of how to post on facebook page wall? 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).

我也尝试使用应用程序解决方案,我成功获得了OAuthAppAccessToken,但这还不够。

I also tried using app solution and I succesfully got OAuthAppAccessToken, but it's not enough.

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.

推荐答案

选项1(通过工具获取令牌) / strong>

Option 1 (get token via tools)

此选项需要通过图形API工具手动输入和复制生成的令牌。我不打算这个选项,因为这两个链接
获取Facebook页面访问令牌的4步程序使用RestFB api发布到Facebook页面墙

This option requires manual entering and copying generated tokens via graph api tools. 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.

选项2(一键式解决方案)

现在这是一个非常自动化的解决方案(如果你像我一样),你想要的。因为我不能告诉我的客户:去这里,复制这个,gimme这个和东西...。我需要做最用户友好的解决方案。最后我实现了FB登录按钮和简单的ajax调用,将获得长期的访问令牌。有了这个记号,我们的应用可以在发生某些事件时自动发布在他的页面上。使用获取Facebook页面访问令牌4步骤程序教程在这里是解决方案:

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. 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:


  1. 使您的应用程序 https://developers.facebook.com/apps/ (您可能需要添加网站平台并使其生效)。

  2. 在仪表板检索应用程序ID和应用程序密码。

  3. 在您的网站上实现登录按钮。有关这方面的很好的信息可以在这里找到 fb登录网站。您可以使用代码片段,只需将应用ID替换为您的应用ID。

  4. 在登录按钮中添加范围,以便我们可以获取页面以及发布操作的权限。 li>
  1. Make your application https://developers.facebook.com/apps/ (you may need to add website platform and make it live).
  2. In dashboard retrieve app id and app secret.
  3. Implement log in button on your website. A great info about this can be found here fb login for web. Code snippet there is all you need, just replace app id with your app 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_actionsonlogin =checkLoginState );>
< / fb:login-button>


  1. 在登录按钮中,您可以看到每次调用登录时调用。在这个函数中,我们可以从FB获得令牌的回复信息,我们需要关于用户的信息(在这种情况下,这只是我们需要的令牌)。以下JavaScript代码通过ajax将用户令牌(短命)发送到我们的服务器。

  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). Following javascript code sends user token (short lived) via ajax, to our server.

function checkLoginState(){
FB.getLoginStatus (function(response){
statusChangeCallback(response);
});
}
函数statusChangeCallback(response){
if(response.status ==='connected'){
getLongLivedToken(response.authResponse.accessToken);
}
}
函数getLongLivedToken(access){
var data = {
$ {fbParam}:acces
};
$ .post(
'$ {fbUrl}',
数据,
函数(INFO){
console.log(done);
},
'text'
);
}

下一步是服务器端。目前,我们收到令牌,我们需要将其转换为长寿命。

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


  • 现在我们需要获取我们要发布的页面的访问令牌的最后一步。从这一点上我们可以使用facebook4j。

  • 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.

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


  • 利润:有了这个标记,我们可以发布在所需页面上:

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


  • 注意:此解决方案不是用作垃圾邮件发送者的页面。用户需要知道哪些事件会触发在他的页面上发布。如果用户想要减少/删除权限,他可以在FB设置中执行。

    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.

    这篇关于用facebook4j发布在页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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