awesome
awesome

Reputation: 317

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). I'm using spring social as framework.

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. The brand page is this kind of page people "like", something similar as 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. 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();

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. 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); 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; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause org.springframework.web.client.HttpClientErrorException: 400 Bad Request

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!

Upvotes: 2

Views: 4324

Answers (1)

Erich
Erich

Reputation: 2773

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

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.

Upvotes: 1

Related Questions