์ด๋ฒ post์์๋ project๋ฅผ ์งํํ๋ฉด์ ์๊ฒ๋ KakaoSocialLogIn์ ๋ํด ์ค๋ช ํ๊ณ ์ ํ๋ค
Kakao ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ ํ๋ฉด ๋ก๊ทธ์ธ ๊ณ์ ์ ๋ณด๋ก web page service์ ์ฐ๊ฒฐํ๊ณ ๋ง์ฝ ๊ณ์ ์ ๋ณด๊ฐ ํด๋น web page์ ๋ฑ๋ก(๊ฐ์ )์ด ๋์ด์์ผ๋ฉด ๋ก๊ทธ์ธ์ด ๋๊ฒ ํ๊ณ , ๋ง์ฝ ๋ฑ๋ก์ด ์๋์ด ์์ผ๋ฉด ๊ฐ์ ํ ๋ก๊ทธ์ธ ๋๋ process์ด๋ค.
- Front-end
- F/E(client)์์ OAuth๋ฅผ ํตํด kakaotalk API server์ผ๋ก ์ธ์ฆ ์ฝ๋ ์์ฒญ
- Kakao Server์์ ์ธ์ฆ ์ฝ๋ ์ ๋ฌ
- F/E์์ ๋ฐ์ ์ธ์ฆ ์ฝ๋๋ก Kakao server์ Access Token์ ์์ฒญ
- F/E์์ Access Token์ ๋ฐ์ B/E์ Access Token์ ์ ๋ฌ
- Back-end
- F/E๋ก ๋ถํฐ ๋ฐ์ Access Token์ผ๋ก Kakao server์ ํด๋น Access Token์ ๋ง๋ ์ฌ์ฉ์ ์ ๋ณด ์์ฒญ
- Kakao server๋ก ๋ถํฐ ์ฌ์ฉ์ ์ ๋ณด์ ๋ํด ์์ฒญ ๋ฐ์ web page ๋ด๋ถ์์ ์ฌ์ฉ์ ํ์ธ ์ฉ์ผ๋ก ์ฌ์ฉ๋ Access Token ์์ฑ(web page์ ๋ฑ๋ก์ด ์๋ ๊ฒฝ์ฐ ์ฌ์ฉ์ ๋ฑ๋ก ํ Token ๋ฐ๊ธ, ๊ธฐ์กด์ ๋ฑ๋ก์ด ๋์ด ์๋ ์ฌ์ฉ์์ธ ๊ฒฝ์ฐ ์ ๋ณด ํ์ธ ํ Token ๋ฐ๊ธ)
F/E๋ก ๋ถํฐ Access Token ๋ฐ๊ธฐ
class KakaotalkSignInView(View): def get(self, request): try : access_token = request.headers.get('Authorization')
B/E์์ Kakao server๋ก ์ ๋ณด ์์ฒญํด์ ๋ฐ๊ธฐ
profile_information = requests.get( "https://kapi.kakao.com/v2/user/me", headers = {'Authorization' : f'Bearer {access_token}'}, timeout=1 )
"https://kapi.kakao.com/v2/user/me" ์๋ฒ๋ก Access Token์ผ๋ก ์์ฒญ์ ํ๋ฉฐ, requests.get์ ์ฌ์ฉํจ์ผ๋ก์จ Access Token๊ณผ ๊ฐ์ ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ ๋ฐ๋๋ก ์์ฒญํจ.
๋ฐ์ ์ ๋ณด๋ฅผ json ํํ๋ก ์ฒ๋ฆฌํ์ฌ ํ์ํ ์ ๋ณด๋ง ๋ถ๋ฌ ์ฌ์ฉํจ
profile_information_json = profile_information.json() profile_id = profile_information_json["id"] email = profile_information_json["kakao_account"]["email"] nickName = profile_information_json["properties"]["nickname"] profile_image = profile_information_json["kakao_account"]["profile"]["profile_image_url"]
import json, jwt, requests
from django.http import JsonResponse
from django.views import View
from django.conf import settings
from .models import User
class KakaotalkSignInView(View):
def get(self, request):
try :
access_token = request.headers.get('Authorization')
if not 'Authorization':
return JsonResponse({"message" : "No_access_token"}, status=404)
profile_information = requests.get(
"https://kapi.kakao.com/v2/user/me",
headers = {'Authorization' : f'Bearer {access_token}'},
timeout=5
)
profile_information_json = profile_information.json()
profile_id = profile_information_json["id"]
email = profile_information_json["kakao_account"]["email"]
nickName = profile_information_json["properties"]["nickname"]
profile_image = profile_information_json["kakao_account"]["profile"]["profile_image_url"]
user, created = User.objects.get_or_create(
social_id = profile_id,
nickname = nickName,
email = email,
profile_image = profile_image)
jwt_token = jwt.encode({"id" : user.id}, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
HttpStatusCode = 201 if created else 200
return JsonResponse({"Token" : jwt_token}, status = HttpStatusCode)
except KeyError:
return JsonResponse({"message" : "Key_Error"}, status = 401)
except User.DoesNotExist:
return JsonResponse({"message" : "Does_Not_Exist"}, status=404)
์ด๋ฒ ํฌ์คํ ์์๋ kakao social ๋ก๊ทธ์ธ ๋ฐฉ๋ฒ์ back-end ํํธ์ ๋ํด์ ์ ๋ฆฌํด ๋ณด์๋ค. ์ ๋ฐ์ ์ผ๋ก process๋ฅผ ์ ๋ฆฌ๋ ์๋ฃ๋ค์ ๋ช๋ช ๋ณด์์ผ๋, back-end ์ ๋ณด์ ๋ํด์ ์ ๋ฆฌ๋ ๊ฒ์ ์ ๋ณด์ง ๋ชปํด์ back-end์์ ํ์ํ ๋ถ๋ถ๋ง ์ ๋ฆฌํด ๋ณด์๋ค. ์์ ๋ก๊ทธ์ธ์ด ์ด๋ป๊ฒ ์งํ๋๋ ์ง ๊ฐ์ด ์์กํ์๋๋ฐ kakao developers์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํจ์ผ๋ก์จ ํ๋ฆ์ด ์ดํด๊ฐ ๋์๊ณ ๊ทธ๋ฅผ ๋ฐํ์ผ๋ก ๊ฒ์ ๋ฐ ์ฌ๋ฌ feed back์ ํตํด ๊ตฌํ์ ์์ฑํ๊ฒ ๋์๋ค. ์ข ๋ ์ถ๊ฐ์ ์ผ๋ก ์์ ์ฌํญ์ด ์์ ์ ์๊ฒ ์ง๋ง ํด๋น code๋ก๋ ๊ตฌํ ๋๋ ๊ฒ์ ํ์ธํ์ ๋ ์ข ๊ธฐ๋ปค๋ค.
์ฐธ๊ณ ๋ฌธํ : https://developers.kakao.com/docs/latest/ko/kakaologin/common