from flask import Flask, render_template, redirect, url_for, request
import requests
import json
app = Flask(name)
@app.route(’/’)
def hello_world():
return render_template(‘index.html’)
@app.route(’/oauth’)
def oauth():
code = str(request.args.get(‘code’))
url = "https://kauth.kakao.com/oauth/token"
payload = “grant_type=authorization_code&client_id=43e3daf58dd14d049001ebdbd6538f58&redirect_uri=http://127.0.0.1:5000/oauth&code=”+str(code)
headers = {
‘Content-Type’: “application/x-www-form-urlencoded”,
‘Cache-Control’: “no-cache”,
}
response = requests.request(“POST”,url,data=payload,headers=headers)
access_token = json.loads(((response.text).encode(‘utf-8’)))[‘access_token’]
url = "https://kapi.kakao.com/v1/user/signup"
headers.update({'Authorization' : "Bearer " + str(access_token)})
response = requests.request("POST",url,headers=headers)
url = "https://kapi.kakao.com/v1/user/me"
profile_request = requests.get("https://kapi.kakao.com/v2/user/me", headers={"Authorization" : f"Bearer {access_token}"},
)
profile_json = profile_request.json()
kakao_account = profile_json.get("kakao_account")
email = kakao_account.get("email", None)
kakao_id = profile_json.get("id")
txt = 'hello kakao'
req_url = 'https://kapi.kakao.com/v2/api/talk/memo/default/send'
headers = {"Content-Type": "application/json","Content-Type": "x-www-form-ur;emcoded","Authorization": 'Bearer ' + str(access_token)}
req = requests.post(req_url, headers=headers, params=make_message(txt))
if req.status_code == 200:
return ("sucess")
else:
return (req.text)
def make_message(text):
temp = {
“object_type”: “text”,
“text”: text,
“link”: {
“web_url”: “https://developers.kakao.com”,
“mobile_web_url”: “https://developers.kakao.com”
},
“button_title”: “바로 확인”,
}
return {"template_object":json.dumps(temp)}
if name == ‘main’:
app.run(debug=True)
위에 작성한 코드로 실행하였는데
{“msg”:“insufficient scopes.”,“code”:-402,“api_type”:“TALK_MEMO_DEFAULT_SEND”,“required_scopes”:[“talk_message”],“allowed_scopes”:[“age_range”,“birthday”,“account_email”,“gender”,“profile”]}
이렇게 권한이 없다고 나옵니다…
엑세스 토큰도 문제 없는것 같은데 뭐가 문제인지 모르겠습니다 ㅜㅜ