Calling Servlet for Facebook API to get Access Token

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

QUESTION EDITED:

I am using Captain Casa framework.

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.

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

But how am I able to call this HttpServlet?

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

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

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>  

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

回答1:

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

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!