본문으로 바로가기
반응형



## 글을 읽기전 참고 해주세요. ##

우선, 이전 게시글을 통해 Kakao Develope에 가입 및 애플리케이션 등록을 하지 않았다면 아래의 링크를 통해 설정 및 확인을 해주세요.

[Spring] Kakao Rest API를 이용해 회원가입을 해보자! - Kako Develope 설정을 하자


## 시작 ##

우선, STS를 실행 시키고 메이븐 프로젝트를 만들어주세요.

그런뒤, POM.XML을 열어주세요.


POM.XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        <!-- 잭슨 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>
 
        <!-- httpcore(카카오) -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
        </dependency>
 
        <!-- httpclient(카카오) -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
cs

POM.XML 디펜던시를 추가 해주세요.


소ㅅ코드를 추가 한 결과


혹시 모르니 스프링 프레임워크 버전을 5.0.6.RELEASE로 해주세요.


이제, 클래스를 하나 만들어 주신 다음에 열어주세요.

필자는 kakao_restapi.java로 만들었습니다.

아래의 소스코드를 추가 해주세요.


kakao_restapi.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public JsonNode getAccessToken(String autorize_code) {
 
        final String RequestUrl = "https://kauth.kakao.com/oauth/token";
 
        final List<NameValuePair> postParams = new ArrayList<NameValuePair>();
 
        postParams.add(new BasicNameValuePair("grant_type", "authorization_code"));
 
        postParams.add(new BasicNameValuePair("client_id", "자신의RESTAPIKEY"));
 
        postParams.add(new BasicNameValuePair("redirect_uri", "http://아이피/최상위경로/oauth"));
 
        postParams.add(new BasicNameValuePair("code", autorize_code));
 
        final HttpClient client = HttpClientBuilder.create().build();
 
        final HttpPost post = new HttpPost(RequestUrl);
 
        JsonNode returnNode = null;
 
        try {
 
            post.setEntity(new UrlEncodedFormEntity(postParams));
 
            final HttpResponse response = client.execute(post);
 
            ObjectMapper mapper = new ObjectMapper();
 
            returnNode = mapper.readTree(response.getEntity().getContent());
 
        } catch (UnsupportedEncodingException e) {
 
            e.printStackTrace();
 
        } catch (ClientProtocolException e) {
 
            e.printStackTrace();
 
        } catch (IOException e) {
 
            e.printStackTrace();
 
        } finally {
 
        }
 
        return returnNode;
 
    }
cs

파란색으로 칠한곳을 확인 해주세요.


REST API키는 위에 있는것

아이피는 하단에 적어둔 사이트 도메인을 확인 해주시길 바랍니다.


POM.XML(최상위 경로)

하얀색으로 칸을 쳐둔것이 자신의 최상위 폴더 입니다.



자신의 컨트롤러.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.kakao.api;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    
    private kakao_restapi kakao_restapi = new kakao_restapi();
    
    @RequestMapping(value = "/oauth", produces = "application/json", method = { RequestMethod.GET, RequestMethod.POST })
    public String kakaoLogin(@RequestParam("code"String code) {
        System.out.println(access_token);
        return "home";
    }
    
}
 
cs

위 소스코드는 예제 입니다.



제가 잘못 알려드린 부분이 있어서 정정을 하겠습니다.

다시, 카카오 개발자로 가신뒤 -> 일반을 눌러주세요.


여기서 리다이렉션 패치를 설정 하실때, 자신의 /최상위폴더/oauth로 변경을 해주세요.


※ 가장 중요한 부분 입니다. ※

https://kauth.kakao.com/oauth/authorize?client_id=자신의RESTAPI키&redirect_uri=리다이렉트주소&response_type=code


필자 기준

https://kauth.kakao.com/oauth/authorize?client_id=0491b56962efe03ab990d7b5104a5b0e&redirect_uri=http://localhost/kakao_restapi/oauth&response_type=code




위 주소를 인터넷창에 작성 해주세요.

사으ㅌ트에 접속을 해주세요.


## 결과 ##





다음 게시글은 RESTAPI를 이용한 회원 가입을 하겠습니다.


반응형