기본소스.
<p><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "com.restfb.exception.FacebookOAuthException" %>
<%@ page import = "com.restfb.Parameter" %>
<%@ page import = "com.restfb.types.FacebookType" %>
<%@ page import = "com.restfb.DefaultFacebookClient" %>
<%@ page import = "java.util.List" %>
<%@ page import = "com.restfb.types.User" %>
<%
String accesstoken = "CAACxGO06MiUBAEFUsjdbfwNdkXoanjSgcAuXlIZB6Wy4TDW5meN3QHAZANoRPRIfeoIHgmy5JF7O2Gma0ndJWDiZCFZC4C8rutLlgwUA45XoKKi0dMCdLj1OiYEbCdKo07oRbpWQ2aBXc0ZAXGqFqBKrJ5B1YZBPYZD&expires=5177265";
DefaultFacebookClient fbClient = new DefaultFacebookClient(accesstoken);
List<User> friends = null;
try{
friends = fbClient.fetchConnection("me/friends", User.class).getData();
}catch(FacebookOAuthException e){
e.printStackTrace();
}
%>
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
< title>Insert title here</title>
< /head>
< body>
친구의 수: <%=friends.size() %>
< /body>
< /html>
< /p>
-------------------------------------------------에러---------------------------------------------
com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: Malformed access token CAACxGO06MiUBAEFUsjdbfwNdkXoanjSgcAuXlIZB6Wy4TDW5meN3QHAZANoRPRIfeoIHgmy5JF7O2Gma0ndJWDiZCFZC4C8rutLlgwUA45XoKKi0dMCdLj1OiYEbCdKo07oRbpWQ2aBXc0ZAXGqFqBKrJ5B1YZBPYZD&expires=5177265
at com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookClient.java:766)
at com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:688)
at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:630)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:592)
at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:556)
at com.restfb.DefaultFacebookClient.fetchConnection(DefaultFacebookClient.java:219)
at org.apache.jsp.face3_jsp._jspService(face3_jsp.java:73)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
2013. 5. 22 오후 6:24:05 org.apache.catalina.core.StandardWrapperValve invoke
심각: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.face3_jsp._jspService(face3_jsp.java:87)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
--------------------------------------------------------------------------------------------------대충 해석을 해보았습니다. 위에 검은색 글씨의 에러부분은 트라이 캐치에서 찍어낸 에러입니다.
해석을 해보면. 받아온 accessToken이 잘못 되었다는 뜻 같습니다..(위,보라색부분)
그래서 다시 로그인 인증을 통해 accessToken을 다시 받고 새로운 토큰을 넣고 돌려보았는데도
계속 저 에러가 나더군요.. 그렇기에 토큰이 잘못되어 아래 널포인트를 야기 시키는것같습니다..
로그인 인증으로 인해 얻은 토큰의 사용법이 잘못되었을까요?
왜 저렇게 에러가 날까요..??
-----------------------------------------------------------------------------------------------
<%@ page language = "java" contentType ="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page import = "java.net.*" %>
<%
String appID = "194720617345573";
String callbackUrl = "http://192.168.1.119:9090/face/face2.jsp";
String oauthUrl = "https://www.facebook.com/dialog/oauth?" +
"client_id="+appID+"&redirect_uri="+URLEncoder.encode(callbackUrl, "UTF-8")+
"&scope=user_about_me, publish_stream, read_friendlists, offline_access";;
response.sendRedirect(oauthUrl);
%>
-------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.net.*, java.io.*" %>
<%
String code = request.getParameter("code");
String appID = "194720617345573";
String appSecret = "692ea36ea932c472383c5746891b27b1";
String callbackUrl = "http://192.168.1.119:9090/face/face2.jsp";
String access_token_url = "https://graph.facebook.com/oauth/access_token?"
+"client_id="+appID
+"&redirect_uri="+URLEncoder.encode(callbackUrl, "UTF-8")
+"&client_secret="+appSecret+"&code="+code;
URL yahoo = new URL(access_token_url);
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
/* HttpURLConnection httpConn = (HttpURLConnection)yc;
InputStream is;
if(httpConn.getResponseCode() >= 400){
is = httpConn.getErrorStream();
}else{
is = httpConn.getErrorStream();
} */
String inputLine = null;
out.println("<br/>access_token respons=======================================");
while((inputLine = in.readLine()) != null)
out.println(inputLine);
out.println("<br/>access_token respons=======================================");
in.close();
%>
<br />code:<%=code %> <br />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
--------------------------------------------------위에 두개가 토큰을 받아오는.. jsp입니당..
답변 감사드립니다. ^-^ㅎㅎ
일반 사이트일 경우 url이 페이스북 앱 설정에 등록만 되어있으면 문제없을것이라 보여집니다~
캔버스나, 탭 용 앱일경우 https 접속주소를 함께 넣어야 하는데, 이땐 인증서가 서버에 설치되어 있어야 합니다.
인증서 발급은 도메인별로 발급이 될것이고 도메인이 필요하겠죠.
정말감사합니다. 토큰 뒤엣부분 지우니까 원하는 답이 나오네요 @_@
아픙로 자주 들리겠습니다. ㅎㅎ
그런데 callbackURL을 그냥 단순히 제 개인 아이피로 저렇게 해도 상관은 없는건가요??
답변 감사드립니다.
그러면 토큰 이 뒷부분만 제거해주면 될까요?
다른데는 이상이 없는것인가요?
한가지지 질문을 드리자면..
callbackURL을 저런식으로 써줘도 되는지 궁금합니다. ㅜ
토큰 문제입니다. 토큰 뒷 부분 "&expires=5177265" 이부분은 토큰값이 아닙니다. 만료시간 옵션입니다.
토큰은 기본적으로 만료시간이 있기때문에 저장해두는 방식은 예기치 못한 오류를 발생시킬 가능성이 있기 때문에 계속 새로 받아주시는 것이 좋을듯 합니다.