简体   繁体   English

如何在Spring Social上以编程方式发布在Facebook页面上?

[英]how to programmatically post on facebook page with spring social?

I'm desperately trying to make automatic posts on a facebook page (brand page, and not a user wall). 我拼命试图在Facebook 页面 (品牌页面,而不是用户界面)上发布自动帖子。 I'm using spring social as framework. 我正在使用Spring Social作为框架。

The idea is the following: I've a web app using spring social and I want to be able to post status programatically on the related facebook brand page. 想法如下:我有一个使用Spring Social的Web应用程序,我希望能够以编程方式在相关的Facebook品牌页面上发布状态。 The brand page is this kind of page people "like", something similar as https://www.facebook.com/cocacola 品牌页面就是人们“喜欢”的这种页面,类似于https://www.facebook.com/cocacola

I tried the following: 1. get an access token of a valid user with admin right on the page 2. get access (how?) to the page, and post on that page. 我尝试了以下操作:1.在页面上直接使用admin获得有效用户的访问令牌2.在页面上进行访问(如何?),然后在该页面上发布。 Maybe via pageoperation? 也许通过页面操作? or something... 或者其他的东西...

I'm stuck on the 1st step, and I was not able to try the 2nd step. 我停留在第一步,但无法尝试第二步。 For the 1st step, I have: 对于第一步,我有:

FacebookConnectionFactory connectionFactory = new FacebookConnectionFactory("clientid", "secretkey");
OAuth2Operations op = connectionFactory.getOAuthOperations();

OAuth2Parameters params = new OAuth2Parameters();
  • what should I put as parameters? 我应该把什么作为参数? I guess I've to define a scope, right? 我想我必须定义一个范围,对吗?
  • which one will be okay to post on that page? 哪一个可以发布在该页面上?

Then, I'm supposed to get an access token for the admin user, as follows: 然后,我应该为管理员用户获取访问令牌,如下所示:

AccessGrant ag = op.exchangeCredentialsForAccess("username", "password", params);

accessToken = ag.getAccessToken();

Here I'm supposed to have an access token for a user in such a way that the user does not need to input himself the username/password via the facebook login page. 在这里,我应该为用户提供访问令牌,以使用户不需要通过facebook登录页面输入用户名/密码。 However, it doesn't work. 但是,它不起作用。 I've the following exception: 我有以下例外:

WARN : org.springframework.web.client.RestTemplate - POST request for " https://graph.facebook.com/oauth/access_token " resulted in 400 (Bad Request); 警告:org.springframework.web.client.RestTemplate-对“ https://graph.facebook.com/oauth/access_token ”的POST请求导致400(错误请求); invoking error handler 调用错误处理程序

Oct 23, 2013 9:57:59 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [appServlet] in context with path [/myapp] threw exception [Request processing failed; 2013年10月23日晚上9:57:59 org.apache.catalina.core.StandardWrapperValve在路径[/ myapp]的上下文中为Servlet [appServlet]调用SEVERE:Servlet.service()引发异常[请求处理失败; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause org.springframework.web.client.HttpClientErrorException: 400 Bad Request 嵌套的异常是org.springframework.web.client.HttpClientErrorException:400错误请求],根本原因是org.springframework.web.client.HttpClientErrorException:400错误请求

I would appreciate if you can post a fully functional proof of concept (I found elements of answer out there, but I didn't manage to make it work...). 如果您能发布功能齐全的概念证明,我将不胜感激(我在那里找到了答案的要素,但我没有设法使其有效……)。

Thanks for your help! 谢谢你的帮助!

Check the documentation on how to authenticate then try this: 查看有关如何进行身份验证的文档 ,然后尝试以下操作:

import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.facebook.api.Facebook;

...

@Inject
private UsersConnectionRepository usersConnectionRepository;

public void postStatusOnPage(String userId, String pageId, String message) {
    ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(userId);
    Connection<Facebook> facebookConnec = connectionRepository.getPrimaryConnection(Facebook.class);
    Facebook facebook = facebookConnec.getApi();
    facebook.pageOperations().post(pageId, message);
}

You'll need the 'manage_pages' privilege and to know the ID of the page you want to post to or you can get the pages the user has access 您将需要'manage_pages'特权,并知道要发布到的页面的ID,否则您将获得用户有权访问的页面

PageOperations pageOps = facebook.pageOperations();
for(org.springframework.social.facebook.api.Account account : pageOps.getAccounts()) {
    ...

If someone figures out how to post a photo to a page, let me know. 如果有人想出如何在页面上发布照片,请告诉我。 Dying there. 死在那里。

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

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