질문많이 해도 되나요??^^ 답변을 너무 잘해주셔서 의지하고 있습니다. 감사합니다.ㅠㅠ
index페이지에서 callback해서 토큰값을 갱신했습니다.
callback 화면에서 갱신된 값을 디비에 저장해서 사용하고 싶은데. callback.jsp 가능할까요?
1. index.jsp
<%@ 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" %>
<%@ page import = "java.net.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>twitter api</title>
</head>
<body>
fb api<br/>
<%
String appID = "164358633992818";
String callbackUrl = "http://" + request.getServerName() + "/facebook/callback.jsp";
String oauthUrl = "https://www.facebook.com/dialog/oauth?" +
"client_id="+ appID + "&redirect_uri=" + URLEncoder.encode(callbackUrl, "UTF-8")+
"&scope=offline_access";;
response.sendRedirect(oauthUrl);
%>
</body>
</html>
2. callback.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import ="java.net.*,java.io.*" %>
<%@ page import = "com.restfb.exception.FacebookOAuthException" %>
<%@ page import = "com.restfb.Parameter" %>
<%@ page import = "com.restfb.types.FacebookType" %>
<%@ page import = "com.restfb.DefaultFacebookClient" %>
<%@ page import = "com.restfb.types.User" %>
<%@ include file="/include/const.jsp"%>
<%@ include file="/include/db/db_connect.jsp"%>
<%@ include file="/include/function/function.jsp"%>
<%
String code = request.getParameter("code");
String appID = "164358633992818";
String appSecret = "1ab1d0b51b7788a8cbeaa7e9a6b167e4";
String callbackUrl = "http://localhost/callback.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()));
String accessToken = null;
String accessToken2 = null;
while ((accessToken = in.readLine()) != null)
in.close();
%>
accessToken 값이 나오시나요?
그렇다면 사용중이신 DB의 jdbc 드라이버 설치하시고 DB에 INSERT 쿼리를 날려주시면 됩니다.
// Mysql JDBC 드라이버를 로드 합니다.
Class.forName("com.imaginary.sql.msql.MsqlDriver");
// 데이터베이스 주소 입니다.
String url = "jdbc:msql://www.dbserver.com:1114";
Connection conn = DriverManager.getConnection(url, "user", "password");
Statement statement = conn.createStatement();
statement.executeUpdate("쿼리");