Home » Java » Can't Post a message using Facebook's API, using MVC and Java

Can't Post a message using Facebook's API, using MVC and Java

Posted by: admin October 29, 2018 Leave a comment

Questions:

I was trying to use the facebook API to post a message to share a page I am making as a project to learn to use APIs.
And I’m running into the following problem.

I tried to post and I was redirected to facebook where I accepted a bunch of permisions and then I was redirected to the url “MyUrl/oauth2callback/Facebook?code=A very large code” and got a 404 Not Found error.

I’m not sure what the problem is and I have been trying to find it for the past 3 days, here is the resource I am using:

import org.restlet.resource.ClientResource;

public class FacebookPostResource {
private String uri = "https://graph.facebook.com/me/feed";
private String access_token = null;


public FacebookPostResource(String access_token) {
    this.access_token = access_token;
}

public boolean publishPost(String message){
    String normalizedMessage=message.replace(' ', '+');
    ClientResource cr=new ClientResource(uri+"?access_token="+access_token);
    cr.post("message="+normalizedMessage);
    return true;
}
}

Here is the Controller:

public class FacebookPostController extends HttpServlet {
/**
 * 
 */
private static final long serialVersionUID = -6818025976353856770L;
private static final Logger log = 
Logger.getLogger(FacebookPostController.class.getName());


public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
IOException,ServletException {
    String accessToken=(String)req.getSession().getAttribute("Facebook- 
token");
    if(accessToken!=null && !"".equals(accessToken)){
        FacebookPostResource fbResource=new 
FacebookPostResource(accessToken);
        fbResource.publishPost(req.getParameter("message"));
        req.getRequestDispatcher("/").forward(req,resp);
    }else{
        log.info("Trying to acces to Facebook without an acces token, 
redirecting to OAuth servlet");

req.getRequestDispatcher("/AuthController/Facebook").forward(req,resp);
    }
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws 
IOException,ServletException {
    doGet(req,resp);    
}
}

Here is My scope configuration:

{   
"Facebook":{
    "tokenUrl":"https://graph.facebook.com/v2.8/oauth/access_token",
    "clientId":"MyID",
    "clientSecret":"MySecret" ,
    "authorizationFormUrl":"https://www.facebook.com/v2.8/dialog/oauth",
    "scopes":["user_posts", "user_friends"]
}
}

Here is the JSP where I write the post:

<c:if test='${empty sessionScope["Facebook-token"]}'>
   <c:redirect url = "/AuthController/Facebook"/>
</c:if>

<h1>Publicar Post en Facebook</h1>
<div class="container">

    <p class="message"></p>

    <form action="/facebookPostCreation" method="post">

        Mensaje: <textarea name="message"></textarea>
        <br>
        <div class="bottom_links">
            <button type="submit" class="button">Publicar en 
Facebook</button>
            <button type="button" 
onClick="javascript:window.location.href='index.html'" 
class="button">Cancel</button>
        </div>
    </form>
</div>

And finally here is my web.xml

...
<servlet>        
<servlet-name>FacebookPostCreation</servlet-name>
<servlet-class>aiss.controller.FacebookPostController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FacebookPostCreation</servlet-name>
 <url-pattern>/facebookPostCreation</url-pattern>
</servlet-mapping>

<servlet>
<description></description>
<display-name>FacebookAuthController</display-name>
<servlet-name>FacebookAuthController</servlet-name>
<servlet-class>aiss.controller.oauth.GenericAuthController</servlet-class>
<init-param>
    <param-name>provider</param-name>
    <param-value>Facebook</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FacebookAuthController</servlet-name>
<url-pattern>/AuthController/Facebook</url-pattern>
</servlet-mapping>

<servlet>
<description></description>
<display-name>FacebookOAuth2Callback</display-name>
<servlet-name>FacebookOAuth2Callback</servlet-name>
<servlet-class>aiss.controller.oauth.OAuth2Callback</servlet-class>
<init-param>
    <param-name>provider</param-name>
    <param-value>Facebook</param-value>
</init-param>
<init-param>
    <param-name>onSuccess</param-name>
    <param-value>redirect:/facebookFriendsListing</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>FacebookOAuth2Callback</servlet-name>
<url-pattern>/OAuth2Callback/Facebook</url-pattern>
</servlet-mapping>
Answers: