为Facebook API调用Servlet以获取访问令牌 [英] Calling Servlet for Facebook API to get Access Token

查看:92
本文介绍了为Facebook API调用Servlet以获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题编辑:

我正在使用Captain Casa框架。

I am using Captain Casa framework.

我有一个按钮会打开一个新选项卡,然后转到Facebook登录页面。

I have a button that will open a new tab and go to facebook login page.

public void goTofbPage(javax.faces.event.ActionEvent event){ 
FBConnection fbConnection = new FBConnection(); 
setBrowserUrl(fbConnection.getFBAuthUrl()); 
m_browserTrigger.trigger(); 
}

如果用户成功登录,它将与用户一起重定向到我的页面数据。

If user logged on successfully, it will redirect to my page together with the user's data.

为了从用户那里获取accessToken,我使用了HttpServlet。

In order to get the accessToken from the user I used HttpServlet.

但我怎么能调用这个HttpServlet?

But how am I able to call this HttpServlet?

我真的需要调用它还是会自动运行?

Do I really need to call it or it will run automatically?

我的jsp看起来像这样。

my jsp looks like this.

<!-- ========== CONTENT BEGIN ========== -->
<f:view>
<h:form>
<f:subview id="lgwfacebookg_sv">
<t:beanprocessing id="g_1" >
<t:clienthttpsender id="g_2" />
<t:jshowurl id="g_3" target="_blank" trigger="#{d.lgwfacebook.browserTrigger}" url="#{d.lgwfacebook.browserUrl}" usedesktop="true" />
<t:timer id="g_4" duration="1000" durationtype="regular" />
</t:beanprocessing>
<t:rowtitlebar id="g_5" />
<t:rowheader id="g_6" />
<t:rowbodypane id="g_7" >
<t:row id="g_8" >
<t:label id="g_9" text="#{d.lgwfacebook.name}" />
</t:row>
<t:row id="g_10" >
<t:field id="g_11" text="#{d.lgwfacebook.browserUrl}" width="0" />
</t:row>
<t:row id="g_12" >
<t:button id="g_13" actionListener="#{d.lgwfacebook.onRedirectUrl}" height="10" image="/images/fbimg.png" width="10" />
</t:row>
<t:rowdistance id="g_14" height="20" />
<t:row id="g_15" />
<t:row id="g_16" >
<t:browser id="g_17" url="#{d.lgwfacebook.fbbrowserURL}" />
</t:row>
</t:rowbodypane>
<t:rowstatusbar id="g_18" />
<t:pageaddons id="g_pa"/>
</f:subview>
</h:form>
</f:view>
<!-- ========== CONTENT END ========== -->

我已经将它添加到web.xml中。

I already added it to web.xml too.

<servlet> 
<servlet-name>MainMenu</servlet-name> 
<servlet-class>managedbeans.MainMenu</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>MainMenu</servlet-name> 
<url-pattern>/MainMenu</url-pattern> 
</servlet-mapping> 

我的Servlet代码:

My Servlet code:

public class MainMenu extends HttpServlet{
        private static final long serialVersionUID = 1L;
        private String code="";

        public void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {

            code = req.getParameter("code");
            if (code == null || code.equals("")) {
                throw new RuntimeException(
                        "ERROR: Distadn't get code parameter in callback.");
            }
            FBConnection fbConnection = new FBConnection();
            String accessToken = fbConnection.getAccessToken(code);

            FBGraph fbGraph = new FBGraph(accessToken);
            String graph = fbGraph.getFBGraph();
            Map<String, String> fbProfileData = fbGraph.getGraphData(graph);
            ServletOutputStream out = res.getOutputStream();
            out.println("<div>Welcome "+fbProfileData.get("first_name"));
            out.println("<div>Your Email: "+fbProfileData.get("email"));
        }


推荐答案

如果它是Servlet那么你需要在Servlet容器中运行它,如 Tomcat Jetty

If it's a Servlet then you need to run it inside a Servlet Container, like Tomcat or Jetty.

当Servlet容器运行时,它绑定到特定的IP或主机名和TCP端口,您应该通过该地址访问它。例如,如果您在localhost上运行Tomcat(例如,在开发中)并且它侦听端口8080,那么您可以通过HTTP调用来执行您的servlet

When the Servlet Container is running, it binds to a specific IP or hostname, and TCP port, and you should access it via that address. For example, if you are running Tomcat on localhost (say, while in development) and it listens on port 8080, then you can execute your servlet by making an HTTP call to

http://127.0.0.1:8080/MainMenu

/ MainMenu端点必须与您在 url-pattern

Where the "/MainMenu" endpoint must match the value that you entered in the web.xml deployment descriptor under url-pattern

这篇关于为Facebook API调用Servlet以获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆