현재 카카오 로그인을 구현하고 있는데 아직은 부족해서 너무 어렵습니다…ㅜㅜ!!
이름 : ParkIT
아이디 : 906869
현재 FrontController에 doProcess를 안에 action을 써 java 페이지로 액션이 가능하게 해놨습니다
에러400이 계속 뜨고 있는데 조언 부탁드립니다!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.team2.commons.Action;
import com.team2.commons.ActionForward;
public class KakaocallbackAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println(" M : KakaocallbackAction_execute");
String code = request.getParameter("code");
System.out.println("code :"+code);
// Post요청, x-www-form-urlencoded
String endpoint="https://kauth.kakao.com/oauth/token";
System.out.println(endpoint);
URL url =new URL(endpoint);
String bodyData="grant_type=authorization_code&";
bodyData += "client_id=b2390ef8fbb9a18526ad7fcf59c29f67&";
// bodyData += "redirect_uri=http://localhost:8088/parkIT/Kakaocallback?cmd=callback&";
bodyData += "redirect_uri=http://localhost:8088/parkIT/Kakaocallback";
bodyData += "code="+code;
//Stream 연결
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
System.out.println("conn 연결완료");
//http header 값 넣기
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setDoOutput(true);
//request 하기
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
bw.write(bodyData);
bw.flush();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8")
);
String input="";
StringBuilder sb=new StringBuilder();
while((input=br.readLine())!=null){
sb.append(input);
}
System.out.println(sb.toString());
//Gson으로 파싱
Gson gson=new Gson();
OAuthToken oAuthToken=gson.fromJson(sb.toString(), OAuthToken.class);
return null;
}
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.team2.commons.Action;
import com.team2.commons.ActionForward;
public class Actionrouter implements Action{
private final static String TAG = "KakaoController : ";
private static final long serialVersionUID = 1L;
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
String cmd1 = request.getParameter("cmd1");
System.out.println(TAG + "router : " + cmd1);
ActionForward forward = new ActionForward();
forward.setPath("./callback");
forward.setRedirect(false);
return forward;
}
}