无法使用Facebook的API使用MVC和Java发布消息

时间:2018-05-21 11:45:39

标签: java api jsp facebook-graph-api model-view-controller

我试图使用facebook API发布消息来共享我正在制作的一个页面,以学习使用API​​。 我遇到了以下问题。

我试图发帖,我被重定向到Facebook,我接受了一堆权限,然后我被重定向到网址“MyUrl / oauth2callback / Facebook?code =一个非常大的代码”,并得到404 Not Found错误。

我不确定问题是什么,过去3天我一直试图找到它,这是我正在使用的资源:

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

这是控制器:

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

这是我的范围配置:

{   
"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"]
}
}

这是我写帖子的JSP:

<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>

最后这是我的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>

1 个答案:

答案 0 :(得分:1)

尝试使用我的代码 https://github.com/OswaldoRosalesA/FacebookAPIJava.git

使用Debuger.java进行测试。 我使用Graph API https://developers.facebook.com/docs/graph-api