๋ฐ˜์‘ํ˜•

์นด์นด์˜คํ†ก ์˜คํ”ˆ API / REST API๋ž€?

์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์—์„œ ๋„ค์ด๋ฒ„ ์˜คํ”ˆ API, ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ ์˜คํ”ˆ API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋‹ค๋ค„๋ดค์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌํ•œ ์˜คํ”ˆ API๋ฅผ ์นด์นด์˜คํ†ก๋„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

์นด์นด์˜ค ๋กœ๊ทธ์ธ์ด๋ผ๋“ ๊ฐ€ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์ง€๋„ ์œ„์น˜, ์นด์นด์˜ค ๋‚ด๋น„ ๋“ฑ ์ผ๋ฐ˜์ ์ธ ์†Œ์…œํ†ตํ•ฉ API ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋น„์ง€๋‹ˆ์Šค API ๋ฐ ์ธ๊ณต์ง€๋Šฅ API๊นŒ์ง€๋„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์นด์นด์˜คํ†ก ์˜คํ”ˆ API ์ œ๊ณต ์„œ๋น„์Šค ๋ชฉ๋ก

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์นด์นด์˜คํ†ก ์˜คํ”ˆ API ์ค‘ ๋ฉ”์‹œ์ง€ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ๋™์ž‘์„ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ค€๋น„ ๋‹จ๊ณ„

1. ์นด์นด์˜คํ†ก ์˜คํ”ˆ API ์„œ๋น„์Šค ์‹ ์ฒญํ•˜๊ธฐ

๋”๋ณด๊ธฐ

๋กœ๊ทธ์ธ ํ›„ ์ƒ๋‹จ์˜ ๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ํ‘œ์‹œ๋˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ถ”๊ฐ€ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™”๋ฉด

์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ํ‘œ์‹œ๋˜๋ฉด ์•ฑ ์ด๋ฆ„๊ณผ ์‚ฌ์—…์ž๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์•ฑ ์•„์ด์ฝ˜ ๋“ฑ๋ก์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ถ”๊ฐ€ํ•˜๊ธฐ ํ•ญ๋ชฉ ์ž…๋ ฅ

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ชฉ๋ก์— ์ถ”๊ฐ€๋˜๋ฉด, ์ƒ์„ฑ๋œ ํ•ญ๋ชฉ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ํด๋ฆญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ํ‘œ์‹œ๋˜๊ณ , REST API ํ‚ค ์™ธ์—๋„ ์—ฌ๋Ÿฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์˜ ์ธ์ฆํ‚ค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ค‘๊ฐ„์— ์žˆ๋Š” ํ”Œ๋žซํผ ์„ค์ •ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด ํ™”๋ฉด

์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ํ‘œ์‹œ๋˜๋ฉด Web ํ”Œ๋žซํผ ๋“ฑ๋ก ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋žซํผ ์„ ํƒ ํ™”๋ฉด

์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ์„ ์ž…๋ ฅํ•˜๋ผ๋Š” ํ™”๋ฉด์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์˜ˆ์‹œ๋กœ ์ €์˜ ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ถ”ํ›„ ๋‚˜์˜ฌ ๋งํฌ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์—ฐ๊ฒฐ๋˜๋Š” ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์–ธ์ œ๋“ ์ง€ ์ˆ˜์ • ๋ฐ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ ์ž…๋ ฅ ํ™”๋ฉด

2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •ํ•˜๊ธฐ

๋”๋ณด๊ธฐ

์œ„ ๊ณผ์ •์ด ๋ชจ๋‘ ๋๋‚˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •๋ณด ํ™”๋ฉด์—์„œ REST API ํ‚ค๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ฆํ‚ค ํ™•์ธ ํ™”๋ฉด

๊ทธ๋ฆฌ๊ณ  ํ™”๋ฉด์˜ ์ขŒ์ƒ๋‹จ์— ์•„๋ž˜ ์•„์ด์ฝ˜์„ ํด๋ฆญํ•˜๋ฉด ๋ฉ”๋‰ด๊ฐ€ ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค.

๋ฉ”๋‰ด ์•„์ด์ฝ˜
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฉ”๋‰ด ํ™•์žฅ ํ™”๋ฉด

์ขŒ์ธก์˜ ๋ฉ”๋‰ด์—์„œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ์„ ํด๋ฆญํ•˜๊ณ , ์ƒํƒœ๋ฅผ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.

์นด์นด์˜ค ๋กœ๊ทธ์ธ ํ™œ์„ฑํ™” ํ™”๋ฉด

Redirect URI์—๋Š” ์˜ˆ์‹œ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

Redirect URI ์ž…๋ ฅ ํ™”๋ฉด

์ด๋ฒˆ์—๋Š” ๋™์˜ํ•ญ๋ชฉ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•˜๊ณ , ํ•˜๋‹จ์˜ ์นด์นด์˜คํ†ก ๋ฉ”์‹œ์ง€ ์ „์†ก์˜ ์„ค์ • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

๋™์˜ํ•ญ๋ชฉ ์„ค์ • ํ™”๋ฉด

๊ฐ„๋‹จํ•˜๊ฒŒ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์ •๋„๋กœ๋งŒ ํ…Œ์ŠคํŠธํ•  ๋ชฉ์ ์ด๋ฏ€๋กœ, ์•„๋ž˜์™€ ๊ฐ™์ด ์„ ํƒ ๋™์˜๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ชฉ์ ์—๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋™์˜ ํ•ญ๋ชฉ ์„ค์ • ํ™”๋ฉด

์ขŒ์ธก ๋ฉ”๋‰ด์—์„œ ์นด์นด์˜ค๋งํฌ๋ฅผ ์„ ํƒํ•˜๊ณ , ๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ ๋นŒ๋” ๋ฐ”๋กœ๊ฐ€๊ธฐ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ ๋นŒ๋” ๋ฐ”๋กœ๊ฐ€๊ธฐ ํด๋ฆญ

์•„๋ž˜์™€ ๊ฐ™์€ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋ฉด, ์ขŒ์ธก์˜ ํ…œํ”Œ๋ฆฟ ๋งŒ๋“ค๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์— ๋ณด์ด๋Š” ํ™”๋ฉด์€ ์ด์ „์— ๋งŒ๋“  ํ…œํ”Œ๋ฆฟ์˜ ์„ค์ •์ž…๋‹ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋งŒ๋“ค๊ธฐ

์„ธ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํ…œํ”Œ๋ฆฟ์ด ๋ณด์ด๋Š”๋ฐ์š”. ๋ณธ๋ฌธ์—์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ…์ŠคํŠธ๋งŒ ์ „์†กํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ œ์ผ ์™ผ์ชฝ์˜ FEED ํ•ญ๋ชฉ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ํ•ญ๋ชฉ ์„ ํƒ ํ™”๋ฉด

์•„๋ž˜์™€ ๊ฐ™์ด ํ…œํ”Œ๋ฆฟ์„ ๊พธ๋ฏธ๋Š” ์„ค์ •์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋‚˜ ํ”„๋กœํ•„, ์ œ๋ชฉ๊ณผ ์„ค๋ช… ๊ทธ๋ฆฌ๊ณ  ๋งํฌ ๋“ฑ ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ์ƒ์„ธํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ทจํ–ฅ์— ๋งž๊ฒŒ ์„ค์ •ํ•˜๊ณ  ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ์ƒ์„ธ ์„ค์ • ํ™”๋ฉด

์ด๋ ‡๊ฒŒ ์„ค์ •๋œ ํ…œํ”Œ๋ฆฟ์€ ์ถ”ํ›„ ํ…œํ”Œ๋ฆฟ ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ ํ•˜๊ธฐ

1. C# ํ”„๋กœ์ ํŠธ ์„ค์ •

๋”๋ณด๊ธฐ

๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ExamKakaoTalk ์ด๋ฆ„์˜ Windows Forms ์•ฑ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Windows Forms ์•ฑ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

๊ทธ๋ฆฌ๊ณ  ๋ˆ„๊ฒŸ(NuGet)์—์„œ Newtonsoft.Json ๋ฐ RestSharp์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

Newtonsoft.Json ๋ฐ RestSharp ์„ค์น˜ํ•˜๊ธฐ

์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ์นด์นด์˜ค API ์š”์ฒญ์„ ์œ„ํ•œ ์ธ์ฆํ‚ค, URL ๋“ฑ์„ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. 

์ƒˆ ํ•ญ๋ชฉ ์ถ”๊ฐ€ํ•˜๊ธฐ

์ด๋ฆ„์€ DefineKakao.cs๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

DefineKakao.cs ์ถ”๊ฐ€ํ•˜๊ธฐ

DefineKakao Class์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ์ •์˜ํ•˜์—ฌ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‚ฌ์šฉ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

namespace ExamKakaoTalk
{
    class DefineKakao
    {
        public const string API_KEY = "89d43f60892ee4b8ad1f3a8f1501da67"; //์‚ฌ์šฉ์ž ์ธ์ฆํ‚ค
        public const string TEMPLATE_ID = "44556"; //๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ ์•„์ด๋””
        public const string USER_ID = "528477"; //์‚ฌ์šฉ์ž ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•„์ด๋””
        public const string REDIRECT_URL = "https://www.naver.com/oauth"; //Redirect URL
        
        //Login Request URL: https://kauth.kakao.com/oauth/authorize?response_type=code&client_id={REST_API_KEY}&redirect_uri={REDIRECT_URI}
        public const string LOGIN_URL = "https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=" + API_KEY + "&redirect_uri=" + REDIRECT_URL;

        public const string HOST_OAUTH_URL = "https://kauth.kakao.com";
        public const string HOST_API_URL = "https://kapi.kakao.com";

        public static string USER_TOKEN;
        public static string ACCESS_TOKEN;
    }
}

2. ์นด์นด์˜คํ†ก ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ ๊ตฌํ˜„ํ•˜๊ธฐ

๋”๋ณด๊ธฐ

๋ณธ๋ฌธ์—์„œ ์นด์นด์˜คํ†ก ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋Š” ์•„๋ž˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์ฒ˜๋Ÿผ ๋งŒ๋“ค ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์„ ์œ„ํ•œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ๋™์ž‘

์นด์นด์˜ค ๋กœ๊ทธ์ธ์— ๊ด€ํ•œ ๊ฐœ๋ฐœ ๋ฌธ์„œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

developers.kakao.com/docs/latest/ko/kakaologin/rest-api

 

Kakao Developers

์นด์นด์˜ค API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ด๋ณด์„ธ์š”. ์นด์นด์˜ค ๋กœ๊ทธ์ธ, ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์นœ๊ตฌ API, ์ธ๊ณต์ง€๋Šฅ API ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developers.kakao.com

์นด์นด์˜ค ๋กœ๊ทธ์ธ ๊ตฌํ˜„์„ ์œ„ํ•ด์„œ๋Š” โ‘  ์ธ๊ฐ€ ์ฝ”๋“œ ๋ฐ›๊ธฐ โ‘ก ํ† ํฐ ๋ฐ›๊ธฐ ๋‘ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋จผ์ € ์ธ๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ฐ›๊ณ , ๊ทธ ์ธ๊ฐ€ ์ฝ”๋“œ๋กœ ํ† ํฐ์„ ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์นด์นด์˜ค ๋กœ๊ทธ์ธ ๋™์ž‘์˜ ์ดํ•ด, ์ถœ์ฒ˜: ์นด์นด์˜ค ๋กœ๊ทธ์ธ ๊ฐœ๋ฐœ ๋ฌธ์„œ

์ด์ œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋กœ๊ทธ์ธ ๋™์ž‘์„ ์œ„ํ•œ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ์ถ”๊ฐ€

๊ทธ๋ฆฌ๊ณ  ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์นด์นด๋กœ ๊ณ„์ • ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ํ‘œ์‹œ๋˜๋„๋ก ๋งŒ๋“ค ๊ฑด๋ฐ์š”. ์ด๋ฅผ ์œ„ํ•ด ์ƒˆ๋กœ์šด ์œˆ ํผ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ์— Windows Form ์ถ”๊ฐ€ํ•˜๊ธฐ

FormKakaoLogin.cs ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

Windows Form ์ถ”๊ฐ€ํ•˜๊ธฐ

์ƒ์„ฑ๋œ ํผ์— webBrowser ์ปจํŠธ๋กค์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ปจํŠธ๋กค์˜ ์ด๋ฆ„์€ webBrowserKakaoLogin์ž…๋‹ˆ๋‹ค.

Form์— webBrowser ์ถ”๊ฐ€ํ•˜๊ธฐ

FormKakaoLogin์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

using RestSharp;
using Newtonsoft.Json.Linq;

namespace ExamKakaoTalk
{
    public partial class FormKakaoLogin : Form
    {
        public FormKakaoLogin()
        {
            InitializeComponent();

            webBrowserKakaoLogin.Navigate(DefineKakao.LOGIN_URL);
        }

        private void webBrowserKakaoLogin_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string code = getCode();
            if (code != "")
            {
                DefineKakao.USER_CODE = code;
                DefineKakao.ACCESS_TOKEN = getToken();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        //์ธ๊ฐ€ ์ฝ”๋“œ ์š”์ฒญํ•˜๊ธฐ
        public string getCode()
        {
            string url = webBrowserKakaoLogin.Url.ToString();
            string token = url.Substring(url.IndexOf("=") + 1);

            //์‚ฌ์šฉ์ž๊ฐ€ '๋™์˜ํ•˜๊ณ  ๊ณ„์†ํ•˜๊ธฐ' ์„ ํƒ, ๋กœ๊ทธ์ธ ์ง„ํ–‰ ์‹œ ์‘๋‹ต ํ˜•ํƒœ:
            //{REDIRECT_URI}?code={AUTHORIZE_CODE}
            if (url.CompareTo(DefineKakao.REDIRECT_URL + "?code=" + token) == 0)
            {
                return token;
            }
            else
            {
                return "";
            }
        }

        //ํ† ํฐ ์š”์ฒญํ•˜๊ธฐ
        public string getToken()
        {
            var client = new RestClient(DefineKakao.HOST_OAUTH_URL);

            var request = new RestRequest("/oauth/token", Method.POST);
            request.AddParameter("grant_type", "authorization_code");
            request.AddParameter("client_id", DefineKakao.API_KEY);
            request.AddParameter("redirect_uri", DefineKakao.REDIRECT_URL);
            request.AddParameter("code", DefineKakao.USER_CODE);

            var result = client.Execute(request);
            var json = JObject.Parse(result.Content);

            return json["access_token"].ToString();
        }
    }
}

์ธ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜ ๋ฌธ์„œ์™€ ๊ฐ™์ด Request ํ˜•ํƒœ๋กœ ํ˜ธ์ถœํ•˜๋ฉด {REDIRECT_URI}?code={AUTHORIZE_CODE} ํ˜•ํƒœ๋กœ ์‘๋‹ต์ด ์˜ต๋‹ˆ๋‹ค. ํ•ด๋‹น ์‘๋‹ต ๋ฌธ์ž์—ด์—์„œ {AUTHORIZE_CODE}๋ฅผ ์ถ”์ถœํ•˜๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋™์ž‘์ด getCode()์—์„œ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

'์ธ๊ฐ€ ์ฝ”๋“œ ์–ป๊ธฐ'์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต

๊ทธ๋ฆฌ๊ณ  ํ† ํฐ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜ ๋ฌธ์„œ์ฒ˜๋Ÿผ ์š”์ฒญํ•˜๊ณ , ์‘๋‹ต๋ฐ›์€ JSON ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑ ํ•˜์—ฌ access_token์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

'ํ† ํฐ ์–ป๊ธฐ'์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต

๋ฉ”์ธ Form์˜ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

private void buttonLogInOut_Click(object sender, EventArgs e)
{
	if (buttonLogInOut.Text == "๋กœ๊ทธ์ธ")
	{
		//์นด์นด์˜ค ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ํผ ์ƒ์„ฑํ•˜๊ธฐ
		FormKakaoLogin form = new FormKakaoLogin();
		if (form.ShowDialog() == DialogResult.OK)
		{
			buttonLogInOut.Text = "๋กœ๊ทธ์•„์›ƒ";
		}
	}
	else
	{
		var client = new RestClient(DefineKakao.HOST_API_URL);
		//๋กœ๊ทธ์•„์›ƒ ์š”์ฒญํ•˜๊ธฐ
		var request = new RestRequest("/v1/user/unlink", Method.POST);
		request.AddHeader("Authorization", "bearer " + DefineKakao.ACCESS_TOKEN);

		if (client.Execute(request).IsSuccessful)
		{
			buttonLogInOut.Text = "๋กœ๊ทธ์ธ";
		}
	}
}

๋กœ๊ทธ์ธ ๋™์ž‘์—์„œ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ•œ FormKakaoLogin์˜ ๋™์ž‘์ด ์ด๋ค„์ง€๊ณ , ๋กœ๊ทธ์•„์›ƒ ๋™์ž‘์—์„œ๋Š” ์•„๋ž˜์˜ ๋กœ๊ทธ์•„์›ƒ ์š”์ฒญ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

'๋กœ๊ทธ์•„์›ƒ'์˜ ์š”์ฒญ

์—ฌ๊ธฐ๊นŒ์ง€ ๊ตฌํ˜„ํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ด๋ฏธ์ง€์˜ UI์™€ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ UI์— ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”ํ›„ ๊ณ„์† ์„ค๋ช…ํ•˜๋ฉด์„œ ์ถ”๊ฐ€ํ•  ์˜ˆ์ •์ด๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ํ™”๋ฉด

FormKakaoLogin Form์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ณ„์ • ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ ํ™”๋ฉด

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„œ๋น„์Šค ์ ‘๊ทผ ๊ถŒํ•œ ์ธ์ฆ ํ™”๋ฉด์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋™์˜ํ•˜๊ธฐ ๋˜๋Š” ์นด์นด์˜คํ†ก ๋ฉ”์‹œ์ง€ ์ „์†ก์„ ์ฒดํฌํ•˜๊ณ  ๋™์˜ํ•˜๊ณ  ๊ณ„์†ํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์„œ๋น„์Šค ์ ‘๊ทผ ๊ถŒํ•œ ์ธ์ฆ ํ™”๋ฉด
์ฃผ์˜
 
 

๊ทธ๋Ÿฐ๋ฐ ์•„๋ฌด๋ฆฌ ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•ด๋„ ๊ณ„์† ํšŒ์ƒ‰ ์Œ์˜์œผ๋กœ ํ‘œ์‹œ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” WinForm WebBrowser ์ปจํŠธ๋กค์˜ IE ๋ฒ„์ „์ด ๋„ˆ๋ฌด ๋‚ฎ์•„์„œ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์— ์ฒจ๋ถ€๋œ ์ฝ”๋“œ์—์„œ SetIE11KeyforWebBrowserControl()์— IE11๋กœ ๋™์ž‘ํ•˜๋„๋ก ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ๋ณ€๊ฒฝ ์ฝ”๋“œ๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด์„œ๋Š” Visual Studio๋ฅผ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ ์„ ์ฃผ์˜ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ๋ณ€๊ฒฝ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์ฒดํฌ๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„œ๋น„์Šค ์ ‘๊ทผ ๊ถŒํ•œ ์ธ์ฆ ํ™”๋ฉด

3. ๋‚˜์—๊ฒŒ ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€ / ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€(ํ…œํ”Œ๋ฆฟ) ์ „์†กํ•˜๊ธฐ

๋”๋ณด๊ธฐ

๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€์™€ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด๋‚ด์ง‘๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •์—์„œ ๋ฉ”์‹œ์ง€ ํ…œํ”Œ๋ฆฟ ๋งŒ๋“ค๊ธฐ์—์„œ ์„ค์ •ํ•œ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

ํ…์ŠคํŠธ ํƒ€์ž…์˜ ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€ ์ „์†ก ๊ฒฐ๊ณผ
์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€ ์ „์†ก ๊ฒฐ๊ณผ

๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ์— ๊ด€ํ•œ ๊ฐœ๋ฐœ ๋ฌธ์„œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

developers.kakao.com/docs/latest/ko/message/rest-api

 

Kakao Developers

์นด์นด์˜ค API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ด๋ณด์„ธ์š”. ์นด์นด์˜ค ๋กœ๊ทธ์ธ, ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์นœ๊ตฌ API, ์ธ๊ณต์ง€๋Šฅ API ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developers.kakao.com

์ด์ œ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ๋„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ์„  UI๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์‹œ์ง€ ์ „์†ก ๊ด€๋ จ UI ์ปจํŠธ๋กค ์ถ”๊ฐ€

์ „์†ก ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์„ ํƒ๋œ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์— ๋”ฐ๋ผ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

private void buttonSendMessage_Click(object sender, EventArgs e)
{
	if (radioButtonCustom.Checked == true) //์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ
	{
		sendMessageToMyself(textBoxMessage.Text);
		textBoxMessage.Text = "";
	}
	else //ํ…œํ”Œ๋ฆฟ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ
		sendTemplateMessageToMyself();
}

๊ทธ๋Ÿผ ๋‚˜์—๊ฒŒ ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ๋ถ€ํ„ฐ ์ƒ์„ธํ•˜๊ฒŒ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€๋Š” ํ…์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ํƒ€์ž…์˜ ์š”์ฒญ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํ…์ŠคํŠธ ํƒ€์ž… ๋ณด๋‚ด๊ธฐ

JSON ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์š”์ฒญํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

๋‚˜์—๊ฒŒ ๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์š”์ฒญ๊ณผ ์‘๋‹ต

๊ธฐ๋ณธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ์ฝ”๋“œ๋Š” sendMessagetoMyself()์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

private void sendMessageToMyself(string message)
{
	//JSON ๊ตฌ์„ฑ
	JObject send = new JObject();
	JObject link = new JObject();
	send.Add("object_type", "text");
	send.Add("text", message);
	link.Add("web_url", "https://developers.kakao.com");
	link.Add("mobile_web_url", "https://developers.kakao.com");
	send.Add("link", link);
	send.Add("button_title", "๋งํฌ ์ด๋™");

	//์š”์ฒญ
	var client = new RestClient(DefineKakao.HOST_API_URL);
	var request = new RestRequest("/v2/api/talk/memo/default/send", Method.POST);
	request.AddHeader("Authorization", "bearer " + DefineKakao.ACCESS_TOKEN);
	request.AddParameter("template_object", send);

	if (client.Execute(request).IsSuccessful)
		MessageBox.Show("๋ฉ”์‹œ์ง€ ์ „์†ก ์„ฑ๊ณต!");
	else
		MessageBox.Show("๋ฉ”์‹œ์ง€ ์ „์†ก ์‹คํŒจ!");
}

์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ๋Š” ๋”์šฑ ์‹ฌํ”Œํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ

์•„๋ž˜์™€ ๊ฐ™์ด template_id๋งŒ ๊ตฌ์„ฑํ•˜์—ฌ ์š”์ฒญํ•˜๋ฉด, ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ ํ…œํ”Œ๋ฆฟ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์š”์ฒญ๊ณผ ์‘๋‹ต

์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ์ฝ”๋“œ๋Š” sendTemplateMessageToMyself()์— ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

public void sendTemplateMessageToMyself()
{
	var client = new RestClient(DefineKakao.HOST_API_URL);

	var request = new RestRequest("/v2/api/talk/memo/send", Method.POST);
	request.AddHeader("Authorization", "bearer " + DefineKakao.ACCESS_TOKEN);
	request.AddParameter("template_id", DefineKakao.TEMPLATE_ID);

	if (client.Execute(request).IsSuccessful)
		MessageBox.Show("๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์„ฑ๊ณต");
	else
		MessageBox.Show("๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ์‹คํŒจ");
}

๊ฒฐ๋ก 

๋ณธ๋ฌธ์—์„œ ์†Œ๊ฐœํ•œ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ๋Š” ์นด์นด์˜ค ์˜คํ”ˆ API์˜ ์•„์ฃผ ์ผ๋ถ€๋ถ„์ผ ๋ฟ์ž…๋‹ˆ๋‹ค. ์ด ์™ธ์—๋„ ์นด์นด์˜คํ†ก ์†Œ์…œ, ์Šคํ† ๋ฆฌ, ์ฑ„๋„, ์‹ฑํฌ, ํŽ˜์ด ๋“ฑ ์ •๋ง ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์˜ API๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋ผ๋„ ํ•  ์ค„๋งŒ ์•ˆ๋‹ค๋ฉด, ๋‹ค๋ฅธ ๊ฒƒ๋„ ๊ฐœ๋ฐœํ•˜๋Š”๋ฐ ์ „ํ˜€ ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๋ณธ๋ฌธ์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ์ฒจ๋ถ€๋œ ํŒŒ์ผ์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

ExamKakaoTalk.zip
5.20MB

๋ฐ˜์‘ํ˜•

'Programming > .Net' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[.Net] C#์—์„œ ์—‘์…€(Excel) ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ• (์˜ˆ์ œ ํฌํ•จ)  (1) 2021.05.01
[.Net] C#์œผ๋กœ ๋Œ€์‹ ์ฆ๊ถŒ ์ฃผ์‹ ์ž๋™ ๋งค๋งค ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ - ํฌ๋ ˆ์˜จ ํ”Œ๋Ÿฌ์Šค ์ค€๋น„ ๋‹จ๊ณ„  (0) 2021.04.12
[.Net] C#์œผ๋กœ ๋Œ€์‹ ์ฆ๊ถŒ ์ฃผ์‹ ์ž๋™ ๋งค๋งค ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ  (2) 2021.04.09
[.Net] ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ API ์‚ฌ์šฉํ•˜์—ฌ C#์—์„œ ์‹ค์‹œ๊ฐ„ ๋ฒ„์Šค ์œ„์น˜ ์กฐํšŒํ•˜๊ธฐ(์˜ˆ์ œ ํฌํ•จ)  (0) 2021.03.20
[.Net] C#์—์„œ ๋„ค์ด๋ฒ„ ๋‰ด์Šค REST API๋กœ ์›ํ•˜๋Š” ๋‰ด์Šค๋งŒ ์ˆ˜์ง‘ํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)  (11) 2021.03.18
[.Net] C# ํ•œ๊ธ€๊ณผ ์ปดํ“จํ„ฐ HwpCtrl ์ ‘๊ทผ ํ—ˆ์šฉ ์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๊ธฐ  (1) 2021.03.16
[.Net] C# ํ”„๋กœ๊ทธ๋žจ ๋ฐฐํฌ ์‹œ ์†Œ์Šค ์ฝ”๋“œ ๋ณดํ˜ธ๋ฅผ ์œ„ํ•œ ๋‚œ๋…ํ™”, ConfuserEx ์‚ฌ์šฉ๋ฐฉ๋ฒ•  (5) 2021.03.14