0

I am using Facebook4j to post something on facebook. The following code worked the first time, but now it says: "This authorization code has been used". I would assume, this would give me a new access code everytime I use this, but apperently it does not. How can I tell facebook to give me a new access token instead of the same one over and over?

public class FacebookPublisher extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Facebook facebook = new FacebookFactory().getInstance();
        facebook.setOAuthAppId("appId", "appSecret");
        facebook.setOAuthPermissions("email,manage_pages,publish_pages,publish_actions");
        request.getSession().setAttribute("facebook", facebook);
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/fb1callback");

        response.sendRedirect(facebook.getOAuthAuthorizationURL(callbackURL.toString()));
    }
}

This is the callback servlet and code:

public class FacebookPublisherCallback extends HttpServlet {
    private static final long serialVersionUID = 6305643034487441839L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Facebook facebook = (Facebook) request.getSession().getAttribute("facebook");
        String oauthCode = request.getParameter("code");

        try {
            token = facebook.getOAuthAccessToken(oauthCode).getToken();
            facebook = new FacebookFactory().getInstance();
            facebook.setOAuthAppId("appId", "appSecret");
            facebook.setOAuthPermissions("email,manage_pages,publish_pages,publish_actions");
            facebook.setOAuthAccessToken(new AccessToken(token));
            facebook.postStatusMessage("Hello World from Facebook4J.");
        } catch (FacebookException e) {
            e.printStackTrace();
        }   

    }
}
jan
  • 3,923
  • 9
  • 38
  • 78
  • 1
    Access tokens and the `code` parameter are two different things. After having the user successfully complete login, you get a `code`, that you then have to exchange for an access token (and that can be done only once). The code shown doesn’t seem to make much sense, f.e. the line to set permissions in the second code part seems totally out of place (that would have to happen when login is called, not when posting). – CBroe Jul 03 '15 at 17:34
  • The offical documentation directs to this example code: https://github.com/roundrop/facebook4j-oauth-example. The callback class looks like this: https://github.com/roundrop/facebook4j-oauth-example/blob/master/src/main/java/facebook4j/examples/signin/CallbackServlet.java. So it gets also the access token with the param code like I did. Is that not correct? Could you maybe provide a code snipped that works? – jan Jul 05 '15 at 19:37

0 Answers0