๐Ÿ”ฅ TIL - Day 19

Kim Dae Hyunยท2021๋…„ 10์›” 3์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
21/93

2์ฐจ ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•ด flask์—์„œ ๋‹ค์–‘ํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.
oauth์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ œ๋Œ€๋กœ ์žก๊ณ  ๊ตฌํ˜„ํ•ด๋ณด๋‹ˆ ์„œ๋น„์Šค ์ข…๋ฅ˜์— ๊ด€๊ณ„์—†์ด ๊ณต์‹๋ฌธ์„œ๋งŒ ๋ณด๊ณ  ๊ตฌํ˜„ ๊ฐ€๋Šฅํ–ˆ๋‹ค. (๋ฟŒ๋“ฏ)

์นด์นด์˜ค, ๋„ค์ด๋ฒ„, ํŽ˜์ด์Šค๋ถ, ๊ตฌ๊ธ€์ •๋„๋ฅผ 2์ฐจ ํ”„๋กœ์ ํŠธ์— ์ ์šฉ์‹œ์ผœ ๋ณผ ์ƒ๊ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋‘ ๋ฏธ๋ฆฌ ๊ตฌํ˜„ํ•ด ๋ณผ ์˜ˆ์ •์ด๋‹ค. ์šฐ์„  ์นด์นด์˜ค์™€ ๋„ค์ด๋ฒ„ ๋จผ์ € ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.


๐Ÿ“Œ ํ‚ค ๊ด€๋ฆฌ !!

๊ตฌํ˜„์— ์•ž์„œ API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ Github์„ ์ด์šฉํ•˜๋ ค๋ฉด ํ‚ค ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ ๋‚ด๊ฐ€ ์ฐพ์€ ๊ฒƒ์€ python-decouple ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

pip install python-decouple

setting.ini์— key ์ •์˜
key=value ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ๋‹ค.

[settings]
KAKAO_REST_API=MY_KEY

NAVER_CLIENT=MY_KEY
NAVER_CLIENT_SECRET=MY_KEY

์ •์˜ํ•œ ํ‚ค ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
์šฐ์„  decouple ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ config ๋ฉ”์„œ๋“œ๋ฅผ import ํ•œ๋‹ค.

from decouple import config

๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์€ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๋‹ค.

client_id = config("KAKAO_REST_API")

๐Ÿ“Œ Flask์—์„œ kakao login

ํ๋ฆ„์€ ๋™์ผํ•˜๋‹ค.

  • ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์š”์ฒญ์ด ์˜ค๋ฉด ์นด์นด์˜ค ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™์‹œ์ผœ์ค€๋‹ค.
    ์ด๋•Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ์‹œ ์ง€์ •ํ•œ redirect_uri๋ฅผ ๋ฐ˜๋“œ์‹œ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ํ•ด๋‹น redirect_uri๋กœ ์—‘์„ธ์Šค ํ† ํฐ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ธ์ฆ์ฝ”๋“œ๋ฅผ ์‘๋‹ตํ•ด์ค€๋‹ค.
  • ์ธ์ฆ์ฝ”๋“œ๋ฅผ ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ์ฝ”๋“œ๋กœ ์—‘์„ธ์Šค ํ† ํฐ์„ ์š”์ฒญํ•œ๋‹ค.
  • ์‘๋‹ต๋ฐ›์€ ์—‘์„ธ์Šค ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์š”์ฒญํ•œ๋‹ค.
from flask import redirect, request, jsonify, Blueprint
import requests
from decouple import config

bp = Blueprint("kakao_login", __name__, url_prefix='/')

kakao_client_key = config('KAKAO_REST_API')
redirect_uri = "http://localhost:5000/oauth/kakao/callback"

# ์‚ฌ์šฉ์ž๊ฐ€ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์š”์ฒญ์‹œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™
# ์‚ฌ์šฉ์ž๊ฐ€ ์นด์นด์˜ค์— ์ธ์ฆ ์„ฑ๊ณต์‹œ ์ง€์ •ํ•œ Redirect_URI๋กœ Access_token์„ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ฆํ† ํฐ(Authentication_code)๋ฅผ ์‘๋‹ต๋ฐ›๋Š”๋‹ค.
@bp.route('/oauth/kakao')
def redirect_kakao_login_page():
    kakao_redirect_url = f"https://kauth.kakao.com/oauth/authorize?client_id={kakao_client_key}&redirect_uri={redirect_uri}&response_type=code"
    return redirect(kakao_redirect_url)

# ์ธ์ฆํ† ํฐ์„ ์‘๋‹ต๋ฐ›๊ณ  ์‘๋‹ต๋ฐ›์€ ์ธ์ฆํ† ํฐ์œผ๋กœ Access_token์„ ์š”์ฒญํ•œ ํ›„ ์‘๋‹ต๋ฐ›์€ Access_token์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์š”์ฒญ
@bp.route('/oauth/kakao/callback')
def access():
    code = request.args.get('code')
    access_token_request_url = f"https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id={kakao_client_key}&redirect_uri={redirect_uri}&code={code}"
    response_token = requests.get(access_token_request_url).json()
    token = response_token['access_token']

    user_info_request_url="https://kapi.kakao.com/v2/user/me"
    user_info = requests.get(user_info_request_url, headers={"Authorization":f"Bearer {token}"})

    return jsonify(user_info.json())

์ด์ œ ์ „๋‹ฌ๋ฐ›์€ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ ๋‹นํ•˜๊ฒŒ DB์— ์ €์žฅํ•˜๊ณ  jwt ๋“ฑ์„ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ์ธ์ฆ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.



๐Ÿ“Œ Flask์—์„œ naver login

๊ฑฐ์˜ ์™„์ „ํžˆ ๋™์ผํ•˜๋‹ค..

from flask import Blueprint, request, jsonify, redirect
import requests
from decouple import config

bp = Blueprint("naver_login", __name__, url_prefix="/")

client_id = config("NAVER_CLIENT")
client_secret = config("NAVER_CLIENT_SECRET")
redirect_uri = "http://localhost:5000/oauth/naver/callback"

# localhost:5000/oauth/naver
@bp.route("/oauth/naver")
def redirect_naver_login_page():
    naver_redirect_url=f"https://nid.naver.com/oauth2.0/authorize?response_type=code&client_id={client_id}&state=STATE_STRING&redirect_uri={redirect_uri}"
    return redirect(naver_redirect_url)

@bp.route("/oauth/naver/callback")
def access():
    code = request.args.get('code')

    token_request_url=f"https://nid.naver.com/oauth2.0/token?grant_type=authorization_code&client_id={client_id}&client_secret={client_secret}&code={code}"
    token_response = requests.get(token_request_url)
    token_json = token_response.json()
    token = token_json['access_token']

    user_info_request_url="https://openapi.naver.com/v1/nid/me"
    user_info = requests.get(user_info_request_url, headers={"Authorization":f"Bearer {token}"})

    return jsonify(user_info.json())



๋‹ค์Œ ํ”„๋กœ์ ํŠธ์˜ ์™„์„ฑ๋„๋ฅผ ์œ„ํ•ด ๊ตฌํ˜„์˜ˆ์ • ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๊ณต๋ถ€ ์ค‘์ด๋‹ค.

๋Š˜ ๊ฐœ๋ฐœ๊ณต๋ถ€๋ฅผ ํ•  ๋•Œ ์ด๊ฑฐ ๊ณต๋ถ€ ํ•ด์„œ ์–ธ์ œ ์จ๋จน์ง€ ... ๋ผ๋Š” ๋ง‰์—ฐํ•จ์ด ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ์—๋Š” ๋‹น์žฅ ์จ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ์ด๋Ÿฐ ์ ์ด ๋™๊ธฐ๋ถ€์—ฌ๋กœ ๋‹ค๊ฐ€์˜ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์š”์ฆ˜ ๊ฐœ๋ฐœ ๊ณต๋ถ€ํ•  ๋ง›์ด ๋‚œ๋‹ค ใ…Žใ…Ž ๐Ÿ˜

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€