티스토리 뷰

[3] 사용자 정보 요청

 

  • id: 카카오톡 앱 연결과정에서 발급되는 고유한 id
  • nickname: 카카오톡 또는 카카오스토리의 닉네임 정보
  • profile_image: 480px * 480px ~ 1024px * 1024px 크기의 카카오톡 또는 카카오스토리의 프로필 이미지 URL
  • thumbnail_image: 110px * 110px(카카오톡 썸네일 프로필 이미지) 또는 160px * 213px(카카오스토리 썸네일 프로필 이미지, 160*213 크기의 범위내에서 이미지 비율에 맞게 변경) 크기의 프로필 이미지의 썸네일 URL

[Request]

GET/POST /v1/user/me HTTP/1.1
Host: kapi.kakao.com
Authorization: Bearer {access_token}
Content-type: application/x-www-form-urlencoded;charset=utf-8

[Response]

사용자 정보 요청이 성공하면 응답 바디에 JSON 객체로 아래 값을 포함합니다.

설명타입
id유저의 고유 IDsigned int64
properties사용자의 정보. Json 형태의 key-value.
사용자 정보 키를 지정한 경우는 해당 키에 대한 정보만 포함한다.
String
HTTP/1.1 200 OK
{
    "id":123456789,
    "properties":
    {
        "nickname":"홍길동",
        "thumbnail_image":"http://xxx.kakao.co.kr/.../aaa.jpg",
        "profile_image":"http://xxx.kakao.co.kr/.../bbb.jpg",
        "custom_field1":"23",
        "custom_field2":"여"
        ...
    }
}

사용자 정보를 얻는 함수는 다음과 같다. 매개변수로 사용자 토큰(Access Token, Refresh Token)을 입력해야 한다. 

   public static JsonNode getKakaoUserInfo(String autorize_code) {

 final String RequestUrl = "https://kapi.kakao.com/v1/user/me";

   

    String CLIENT_ID = RestApiKey; // REST API KEY

    String REDIRECT_URI = Redirect_URI; // 리다이렉트 URI

    String code = autorize_code; // 로그인 과정중 얻은 토큰 값


    final HttpClient client = HttpClientBuilder.create().build();

    final HttpPost post = new HttpPost(RequestUrl);

   

    // add header

    post.addHeader("Authorization", "Bearer " + autorize_code);

   

    JsonNode returnNode = null;

   

    try {

      final HttpResponse response = client.execute(post);

      final int responseCode = response.getStatusLine().getStatusCode();


      System.out.println("\nSending 'POST' request to URL : " + RequestUrl);

      System.out.println("Response Code : " + responseCode);


      //JSON 형태 반환값 처리

      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 {

        // clear resources

    }

    return returnNode;

}


컨트롤러에서 다음과 같이 함수를 호출하여 사용자정보를 확인한다. 

  //사용자 정보 요청

        JsonNode userInfo = Kakao.getKakaoUserInfo(accessCode);

        

        // Get id

  String id = userInfo.path("id").asText();

  String nickname = null;

  String thumbnailImage = null;

  String profileImage = null;

  String message = null;

     

        // 유저정보 카톡에서 가져오기 Get properties

JsonNode properties = userInfo.path("properties");

if (properties.isMissingNode()) {

// if "name" node is missing

else {

nickname = properties.path("nickname").asText();

thumbnailImage = properties.path("thumbnail_image").asText();

profileImage = properties.path("profile_image").asText();


System.out.println("nickname : " + nickname);

System.out.println("thumbnailImage : " + thumbnailImage);

System.out.println("profileImage : " + profileImage);

}



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함