diff --git a/prProject/accounts/__pycache__/__init__.cpython-310.pyc b/prProject/accounts/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..a779bc1 Binary files /dev/null and b/prProject/accounts/__pycache__/__init__.cpython-310.pyc differ diff --git a/prProject/accounts/__pycache__/apps.cpython-310.pyc b/prProject/accounts/__pycache__/apps.cpython-310.pyc new file mode 100644 index 0000000..c3b33a1 Binary files /dev/null and b/prProject/accounts/__pycache__/apps.cpython-310.pyc differ diff --git a/prProject/accounts/urls.py b/prProject/accounts/urls.py index e0e59f3..466e560 100644 --- a/prProject/accounts/urls.py +++ b/prProject/accounts/urls.py @@ -1,10 +1,15 @@ +from django.contrib import admin from django.urls import path from accounts import views +from .views import kakao_get_login, get_user_info app_name = "accounts" urlpatterns = [ + path('admin/', admin.site.urls), path('signup/', views.signup, name='signup'), path('login/', views.login, name='login'), path('logout/', views.logout, name='logout'), + path('login/kakao', kakao_get_login), + path('login/kakao/user/callback/', get_user_info, name="kakao_callback"), ] diff --git a/prProject/accounts/views.py b/prProject/accounts/views.py index a01e414..1b5f012 100644 --- a/prProject/accounts/views.py +++ b/prProject/accounts/views.py @@ -1,6 +1,11 @@ from django.shortcuts import render, redirect from django.contrib import auth from django.contrib.auth.models import User +import requests +from prProject.settings import SOCIAL_OUTH_CONFIG +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import AllowAny +from rest_framework.response import Response def signup(request) : if request.method == 'POST' : @@ -28,4 +33,43 @@ def login(request) : def logout(request) : auth.logout(request) - return redirect('home') \ No newline at end of file + return redirect('home') + + +@api_view(['GET']) +@permission_classes([AllowAny, ]) +def kakao_get_login(request): + CLIENT_ID = SOCIAL_OUTH_CONFIG['KAKAO_REST_API_KEY'] + REDIRECT_URL = SOCIAL_OUTH_CONFIG['KAKAO_REDIRECT_URI'] + url = "https://kauth.kakao.com/oauth/authorize?response_type=code&client_id={0}&redirect_uri={1}".format( + CLIENT_ID, REDIRECT_URL) + res = redirect(url) + return res + + +@api_view(['GET']) +@permission_classes([AllowAny, ]) +def get_user_info(reqeust): + CODE = reqeust.query_params['code'] + url = "https://kauth.kakao.com/oauth/token" + res = { + 'grant_type': 'authorization_code', + 'client_id': SOCIAL_OUTH_CONFIG['KAKAO_REST_API_KEY'], + 'redirect_url': SOCIAL_OUTH_CONFIG['KAKAO_REDIRECT_URI'], + 'client_secret': SOCIAL_OUTH_CONFIG['KAKAO_SECRET_KEY'], + 'code': CODE + } + headers = { + 'Content-type': 'application/x-www-form-urlencoded;charset=utf-8' + } + response = requests.post(url, data=res, headers=headers) + token_json = response.json() + user_url = "https://kapi.kakao.com/v2/user/me" + auth = "Bearer " + token_json['access_token'] + HEADER = { + "Authorization": auth, + "Content-type": "application/x-www-form-urlencoded;charset=utf-8" + } + res = requests.get(user_url, headers=HEADER) + print(response.json()) + return Response(res.text) \ No newline at end of file diff --git a/prProject/playlistApp/__pycache__/__init__.cpython-310.pyc b/prProject/playlistApp/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..46c5527 Binary files /dev/null and b/prProject/playlistApp/__pycache__/__init__.cpython-310.pyc differ diff --git a/prProject/playlistApp/__pycache__/apps.cpython-310.pyc b/prProject/playlistApp/__pycache__/apps.cpython-310.pyc new file mode 100644 index 0000000..f4b1804 Binary files /dev/null and b/prProject/playlistApp/__pycache__/apps.cpython-310.pyc differ diff --git a/prProject/prProject/.gitignore b/prProject/prProject/.gitignore new file mode 100644 index 0000000..6fa6719 --- /dev/null +++ b/prProject/prProject/.gitignore @@ -0,0 +1,229 @@ +### Kakao login ### +secrets.json + +# Created by https://www.toptal.com/developers/gitignore/api/macos,django,visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=macos,django,visualstudiocode + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite3 +db.sqlite3-journal +media + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Django.Python Stack ### +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/macos,django,visualstudiocode \ No newline at end of file diff --git a/prProject/prProject/settings.py b/prProject/prProject/settings.py index fae1149..4b0dea3 100644 --- a/prProject/prProject/settings.py +++ b/prProject/prProject/settings.py @@ -12,15 +12,23 @@ from pathlib import Path +import os +import json + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +# 카카오 로그인 +secret_file = os.path.join(BASE_DIR, "prProject/secrets.json") +secrets = None +with open(secret_file) as f: + secrets = json.loads(f.read()) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-5ldzoyxslwf9^l_-)2oi8rt-q31zil2p_$hcpwinx#j-1k75oc' +SECRET_KEY = secrets['SECRET_KEY'] # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -38,9 +46,20 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'accounts', - 'playlistApp' + 'playlistApp', + + # 카카오 로그인 + 'django.contrib.sites', + 'rest_framework', ] +# 카카오 로그인 +SOCIAL_OUTH_CONFIG = { + 'KAKAO_REST_API_KEY': secrets['KAKAO_REST_API_KEY'], + 'KAKAO_REDIRECT_URI': secrets['KAKAO_REDIRECT_URI'], + 'KAKAO_SECRET_KEY': secrets['KAKAO_SECRET_KEY'], +} + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',