프로젝트 일지와 독립적으로 기록하여 기억해두기
도대체 어떻게 통신하는가??
여기서 http로 파이썬의 request를 하는 부분이 나오는데 이 부분을 잘 몰라서 한참 헤맸다.
토큰이 담긴 곳은 당연히 헤더스다. 실제로 맞춰볼 때도 바디에 담겨서 왔었던 적이 있어 제대로 구현이 안 되기도 했다.
POST인 이유:
GET과의 차이점이 드러나는 부분인데 GET은 단순히 데이터를 받아오는기만 하는 것이지 위 과정처럼 토큰을 생성하거나 수정하는 등의 과정이 있을 때는 POST를 쓰는 거이 옳다.
큰 차이 : 바디가 있냐 없냐도 차이가 있다.
이걸 보고 대체 어떻게 하란건지 처음에 굉장히 난처했다. 하지만 http가 어떻게 requests를 통해 송수신 되는지 확인한 결과
공식문서ㅓ
헤더스를 어떻게 위치 시키고 하는지 활용하여 get할 수 있었다.
물론 이는 print를 찍어보면 알겠지만 {{}}형태의 데이터로 온다. 이를 필요한 데이터만큼 []를 이용하여 추출하면 된다.
이는 추후 과제로 두고 정리부터 하면 다음과 같다.
import json
import bcrypt
import jwt
import requests
from django.shortcuts import redirect
from django.views import View
from django.http import JsonResponse, HttpResponse
from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
from my_settings import SECRET_KEY, HASHING_ALGORITHM
from .models import User, Creator
class KakaoSignInView(View):
def post(self,request):
try:
access_token = request.headers.get('Authorization')
user_profile = requests.get(
'https://kapi.kakao.com//v2/user/me',
headers={'Authorization' : 'Bearer {}'.format(access_token)}
)
json_user_profile = user_profile.json()
kakao_id = json_user_profile['id']
nickname = json_user_profile['properties']['nickname']
user_email = json_user_profile['kakao_account']['email']
if not User.objects.filter(kakao_id = kakao_id):
username = User.objects.create(
username = nickname,
email = user_email,
kakao_id = kakao_id
)
new_token = jwt.encode({'username_id' : username.id}, SECRET_KEY, algorithm=HASHING_ALGORITHM)
return JsonResponse({'message' : "SIGN_IN_SUCCESS",'new_token' : new_token},status=200)
username = User.objects.get(kakao_id=kakao_id)
new_token = jwt.encode({'username_id' : username.id}, SECRET_KEY, algorithm=HASHING_ALGORITHM)
return JsonResponse({'message' : "LOGIN_SUCCESS", 'new_token' : new_token}, status=200)
except KeyError:
return JsonResponse({"message" : "KEY_ERROR"}, status=400)
except ObjectDoesNotExist:
return JsonResponse({"message" : "USER_DOES_NOT_EXIST"}, status=400)
고맙습니다 엉엉....