๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Open API] Naver - ๋„ค์ด๋ฒ„ ํŒŒํŒŒ๊ณ  ๋ฒˆ์—ญ API ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(Feat. RestFul Flask) ๋ณธ๋ฌธ

๐Ÿ‘Œ Open API/Naver

[Open API] Naver - ๋„ค์ด๋ฒ„ ํŒŒํŒŒ๊ณ  ๋ฒˆ์—ญ API ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(Feat. RestFul Flask)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 2. 15. 10:00
๋ฐ˜์‘ํ˜•

๋„ค์ด๋ฒ„ ๊ฐœ๋ฐœ API ์ด์šฉ์‹ ์ฒญ์„ ํ•ด์„œ ํด๋ผ์ด์–ธํŠธ ID์™€ ์‹œํฌ๋ฆฟ ํ‚ค๋ฅผ ๋ฐ›์•˜๋‹ค.
์ด์ œ ๋ฒˆ์—ญ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž.

์•„๋ž˜๋Š” ์ด์šฉ์‹ ์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

https://ruriruriya.tistory.com/191

 

[Open API] Naver - ๋„ค์ด๋ฒ„ ๊ฐœ๋ฐœ์ž์„ผํ„ฐ API ์ด์šฉ์‹ ์ฒญ ๋ฐ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก๋ฐฉ๋ฒ•

๋„ค์ด๋ฒ„ API๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋„ค์ด๋ฒ„ ์•„์ด๋””๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ  API ์ด์šฉ์‹ ์ฒญ์„ ํ•˜๊ณ  API ํ‚ค๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์•„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋„ค์ด๋ฒ„ API ์ด์šฉ์‹ ์ฒญ๊ณผ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก์„ ํ•ด๋ณด์ž. 1. ๋„ค์ด๋ฒ„ ๊ฐœ๋ฐœ์ž ์„ผํ„ฐ

ruriruriya.tistory.com

 

1. RestFul  API (Flask)

ํŒŒ์ด์ฌ์œผ๋กœ ์„ค์ •์„ ํ•ด๋ณด์ž.

- app.py

#flask ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ด์šฉํ•œ, Restful API ์„œ๋ฒ„ ๊ฐœ๋ฐœ

from flask import Flask
from flask_jwt_extended import JWTManager
from flask_restful import Api
from config import Config
from resources.naver import ChineseResource, NewsResource

# ๊ธฐ๋ณธ ๊ตฌ์กฐ
app = Flask(__name__)

# ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ธํŒ…
app.config.from_object(Config)


api = Api(app)

api.add_resource(ChineseResource,'/chinese')


if __name__=='__main__':
    app.run()

 

- resources ํŒจํ‚ค์ง€ > naver.py

class ChineseResource(Resource) :

    def post(self) :
        
        data = request.get_json()
        
        # ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž…๋ ฅํ•œ body์˜ json ๊ฐ€์ ธ์˜ค๊ธฐ
        # ๋„ค์ด๋ฒ„์˜ ํŒŒํŒŒ๊ณ  API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ
        # ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

        # ํŒŒํŒŒ๊ณ  API์˜ ๋ฌธ์„œ๋ฅผ ๋ณด๊ณ ,
        # ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์•ผ ํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜์—ฌ
        # requests์˜ get, post, put, delete ๋“ฑ์˜
        # ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

        req_data = {
                        "source":"ko",
                        "target":"zh-CN",
                        "text":data['sentence']
                    }
        
        req_header ={
                        "X-Naver-Client-Id":"7V89Lh5rHWswlUoe6D6B",
                        "X-Naver-Client-Secret":"9l22vo6_9b"

                   }

        response = requests.post('https://openapi.naver.com/v1/papago/n2mt',
                     req_data,
                     headers=req_header)
        
        # ๋’ค์— s ๋ถ™์€ ๊ฒƒ์œผ๋กœ ์œ„์— request๋Š” flask ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž‘ ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
        
        # ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒํŒŒ๊ณ  ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ๋ฐ›์•„์™”์œผ๋‹ˆ, 
        # ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ๋ฝ‘์•„๋‚ด๋ฉด ๋œ๋‹ค.

        print(response)
        
        # ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•ด์„œ๋Š”
        # ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ json์œผ๋กœ ์ €์žฅ โ˜…โ˜…โ˜…โ˜…โ˜…

        response = response.json()

        print()
        print(response)

        chinese = response['message']['result']['translatedText']

        return {"result" : "success",
                "chinese": chinese}, 200

 

2. ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ์š”์ฒญ

ํฌ์ŠคํŠธ๋งจ์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ๊ตญ์–ด๋ฅผ ์ „์†กํ•˜๋ฉด
์ค‘๊ตญ์–ด ๋ฒˆ์—ญ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž˜ ๋ฐ›์•„์˜ค๋Š” ์ง€ ํ…Œ์ŠคํŠธ ํ•ด๋ณด์ž.

- Headers ์—์„œ ํด๋ผ์ด์–ธํŠธ ID์™€ ์‹œํฌ๋ฆฟ ํ‚ค๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

 

- Body์— ๋ฒˆ์—ญํ•  ํ•œ๊ตญ์–ด ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์„œ ์š”์ฒญํ•˜๋ฉด
JSON์œผ๋กœ ์ค‘๊ตญ์–ด๋ฅผ ์‘๋‹ตํ•ด์ค€๋‹ค!

๋ฐ˜์‘ํ˜•