tobby48 4 years ago
parent
commit
f1a9b5b89f

+ 30
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/kakao-test.py View File

@@ -0,0 +1,30 @@
1
+from flask import Flask, render_template, redirect, url_for, request
2
+import requests
3
+import json
4
+app = Flask(__name__)
5
+
6
+@app.route('/')
7
+def index() :
8
+    return render_template('index.html')
9
+
10
+@app.route('/oauth')
11
+def oauth():
12
+    code = str(request.args.get('code'))
13
+    resToken = getAccessToken("1fd8d72c840797ca81faeedb8abdfcfb",str(code))  #XXXXXXXXX 자리에 RESET API KEY값을 사용
14
+    return 'code=' + str(code) + '<br/>response for token=' + str(resToken)
15
+
16
+def getAccessToken(clientId, code) :  # 세션 코드값 code 를 이용해서 ACESS TOKEN과 REFRESH TOKEN을 발급 받음
17
+    url = "https://kauth.kakao.com/oauth/token"
18
+    payload = "grant_type=authorization_code"
19
+    payload += "&client_id=" + clientId
20
+    payload += "&redirect_url=http%3A%2F%2Flocalhost%3A5000%2Foauth&code=" + code
21
+    headers = {
22
+        'Content-Type' : "application/x-www-form-urlencoded",
23
+        'Cache-Control' : "no-cache",
24
+    }
25
+    reponse = requests.request("POST",url,data=payload, headers=headers)
26
+    access_token = json.loads(((reponse.text).encode('utf-8')))
27
+    return access_token
28
+
29
+if __name__ == '__main__' :
30
+    app.run(debug = True)

+ 43
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/kakao-test2.py View File

@@ -0,0 +1,43 @@
1
+import requests
2
+import urllib
3
+import json
4
+
5
+def getAccessToken(refreshToken) :
6
+    url = "https://kauth.kakao.com/oauth/token"
7
+    payload = "grant_type=refresh_token&client_id=1fd8d72c840797ca81faeedb8abdfcfb&refresh_token=" + refreshToken
8
+    headers = {
9
+        'Content-Type' : "application/x-www-form-urlencoded",
10
+        'Cache-Control' : "no-cache",
11
+    }
12
+
13
+    reponse = requests.request("POST",url,data=payload, headers=headers)
14
+    access_token = json.loads(((reponse.text).encode('utf-8')))
15
+    return access_token
16
+
17
+def sendText(accessToken) :
18
+    url = 'https://kapi.kakao.com/v2/api/talk/memo/default/send'
19
+    payloadDict = dict({
20
+            "object_type" : "text",
21
+            "text" :"sample text",
22
+            "link" : {
23
+                "web_url" : "http://localhost:5000",
24
+                "mobile_web_url" : "http://localhost:5000.mobile"
25
+             },
26
+            "button_title" : "button-title",
27
+            })
28
+#     payload = 'template_object=' + str(json.dumps(payloadDict))
29
+    payload = 'template_object=' + str(payloadDict)
30
+    print(payload)
31
+    headers = {
32
+        'Content-Type' : "application/x-www-form-urlencoded",
33
+        'Cache-Control' : "no-cache",
34
+        'Authorization' : "Bearer " + accessToken,
35
+    }
36
+
37
+    reponse = requests.request("POST",url,data=payload, headers=headers)
38
+    access_token = json.loads(((reponse.text).encode('utf-8')))
39
+    return access_token
40
+
41
+result =  getAccessToken("LZDlINbTkVDL-kiJoS_o0nyOPW25MqdhHQaBMgorDKcAAAFzHZWGXg")   # 메세지 받을 사람의 REFRESH TOKEN 이용
42
+print(result['access_token'])
43
+print(sendText(result['access_token']))

+ 25
- 0
src/main/python/kr/co/swh/lecture/opensource/flask/templates/index.html View File

@@ -0,0 +1,25 @@
1
+<!DOCTYPE html>
2
+
3
+<html>
4
+
5
+<head>
6
+
7
+<meta charset="utf-8">
8
+
9
+<title>index</title>
10
+
11
+</head>
12
+
13
+<body>
14
+
15
+<a href="https://kauth.kakao.com/oauth/authorize?client_id=1fd8d72c840797ca81faeedb8abdfcfb&redirect_uri=http://localhost:5000/oauth&response_type=code&scope=talk_message">
16
+
17
+    Kakao login
18
+
19
+</a>
20
+
21
+</body>
22
+
23
+</head>
24
+
25
+</html>