Search code examples
javaspring-socialspring-social-facebook

Spring Social - Facebook integration


I need to integrate spring-social with Facebook, and get the feeds/post from my company page.

Code snippet

public static void main(String[] args) {
        Facebook facebook1 = new FacebookTemplate("");
        FeedOperations feedOperations = facebook1.feedOperations();
        PagedList<Post> feeds = feedOperations.getFeed();

        for (Post post : feeds) {
            System.out.println("@" + post.getName());
        }
    }

I have generated the key on "https://developers.facebook.com", but when i enter the same in FacebookTemplate("");, and runs the program , it throws me an error.

Exception in thread "main" org.springframework.social.InvalidAuthorizationException: Invalid OAuth access token.
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:81)
    at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:616)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:572)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:547)

Please help me in this regard,

1.) Where and how can i generate keys.

2.) How can i get access to user feeds. My secret key might need to get access to that user profile.

3.) Where i need to provide user


Solution

  • Using the FacebookTemplate constructor directly will provide you with an unauthenticated (that is, without OAuth) connection only, that will work for queries that do not require authentication. This is why you get Authorization Exceptions, accessing feeds require authentication (an authenticated facebook user, that has given your application the proper authorization to perform these actions).

    Note that you will need a way to get the user's access token to be authenticated, and doing so in a console application might prove tricky. Using Spring Social in a WebApplication however is quite easy, basically all you need is

    @Controller
    @RequestMapping("/")
    public class HelloController {
    
        private Facebook facebook;
        private ConnectionRepository connectionRepository;
    
        @Inject
        public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
            this.facebook = facebook;
            this.connectionRepository = connectionRepository;
        }
    
        @RequestMapping(method=RequestMethod.GET)
        public String helloFacebook(Model model) {
            if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
                return "redirect:/connect/facebook";
            }
    
            model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
            PagedList<Post> feed = facebook.feedOperations().getFeed();
            model.addAttribute("feed", feed);
            return "hello";
        }
    
    }
    

    (example taken from here: https://github.com/spring-guides/gs-accessing-facebook) with a configuration with spring.social.facebook.appId and spring.social.facebook.appSecret configured properly.

    To test your queries with a console application however, you might

    1. Create app on Facebook's api pages.
    2. Go to https://developers.facebook.com/tools/accesstoken/
    3. Use that tokens in the FacebookTemplate-Constructor

    This will always be your user, so you might not want to release this app into the wild ;)