0

我正在使用 Facebook4j 在 facebook 上发布一些内容。以下代码第一次工作,但现在它说:“此授权代码已被使用”。我会假设,每次我使用它都会给我一个新的访问代码,但显然它没有。我如何告诉 facebook 给我一个新的访问令牌,而不是一遍又一遍地给我一个新的访问令牌?

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

这是回调 servlet 和代码:

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

    }
}
4

0 回答 0