Selaa lähdekoodia

kakao talk 수정

tobby48 3 vuotta sitten
vanhempi
commit
b287fa2e9a

+ 0
- 43
src/kr/co/swh/lecture/python/scene5/kakao/dummy_sample.py Näytä tiedosto

@@ -1,43 +0,0 @@
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("hliBdT6lOqCFUlPzcdXVWEkHlDy3jE24_omX3gopb9QAAAFzPCUkJA")   # 메세지 받을 사람의 REFRESH TOKEN 이용
42
-print(result['access_token'])
43
-print(sendText(result['access_token']))

+ 0
- 30
src/kr/co/swh/lecture/python/scene5/kakao/kakao_auth_request.py Näytä tiedosto

@@ -1,30 +0,0 @@
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)

+ 1
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_code.json Näytä tiedosto

@@ -0,0 +1 @@
1
+{"access_token": "HGrC0iH3cueJVbIHq2oaHp1JIv_dLDhS_KaHQwo9c5sAAAGADIpnAw", "token_type": "bearer", "refresh_token": "tO0ZTG4TQiRuPiVYx0aAi0LmempgjrD_t8YiRAo9c5sAAAGADIpnAg", "expires_in": 21599, "scope": "talk_message", "refresh_token_expires_in": 5183999}

+ 13
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_get_code.py Näytä tiedosto

@@ -0,0 +1,13 @@
1
+from flask import Flask, render_template, redirect, url_for, request
2
+import requests
3
+import json
4
+app = Flask(__name__)
5
+
6
+# 형태 : https://kauth.kakao.com/oauth/authorize?client_id={{REST API}}&redirect_uri={{REDIRECT URL}}&response_type=code&scope=talk_message
7
+# 접속 : https://kauth.kakao.com/oauth/authorize?client_id=xxxx&redirect_uri=http://localhost:5000/oauth&response_type=code&scope=talk_message
8
+@app.route('/oauth')
9
+def oauth():
10
+    return request.args
11
+
12
+if __name__ == '__main__' :
13
+    app.run(debug = True)

+ 24
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_get_token.py Näytä tiedosto

@@ -0,0 +1,24 @@
1
+from flask import Flask, render_template, redirect, url_for, request
2
+import requests
3
+import json
4
+app = Flask(__name__)
5
+
6
+rest_api_key = "1fd8d72c840797ca81faeedb8abdfcfb"
7
+redirect_uri = "http://localhost:5000/oauth"
8
+
9
+# 접속 : https://kauth.kakao.com/oauth/authorize?client_id=xxxx&redirect_uri=http://localhost:5000/oauth&response_type=code&scope=talk_message
10
+@app.route('/oauth')
11
+def oauth():
12
+    url = "https://kauth.kakao.com/oauth/token"
13
+    authorize_code = str(request.args.get('code'))
14
+    print(authorize_code)
15
+    data = { 'grant_type': 'authorization_code', 'client_id': rest_api_key, 'redirect_uri': redirect_uri, 'code': authorize_code } 
16
+    response = requests.post(url, data=data) 
17
+    tokens = response.json() 
18
+    
19
+    with open("kakao_code.json", "w") as fp:
20
+        json.dump(tokens, fp) 
21
+    return tokens
22
+
23
+if __name__ == '__main__' :
24
+    app.run(debug = True)

+ 27
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_get_token_html.py Näytä tiedosto

@@ -0,0 +1,27 @@
1
+from flask import Flask, render_template, redirect, url_for, request
2
+import requests
3
+import json
4
+app = Flask(__name__)
5
+
6
+rest_api_key = "1fd8d72c840797ca81faeedb8abdfcfb"
7
+redirect_uri = "http://localhost:5000/oauth"
8
+
9
+@app.route('/')
10
+def index() :
11
+    return render_template('index.html')
12
+
13
+@app.route('/oauth')
14
+def oauth():
15
+    url = "https://kauth.kakao.com/oauth/token"
16
+    authorize_code = str(request.args.get('code'))
17
+    print(authorize_code)
18
+    data = { 'grant_type': 'authorization_code', 'client_id': rest_api_key, 'redirect_uri': redirect_uri, 'code': authorize_code } 
19
+    response = requests.post(url, data=data) 
20
+    tokens = response.json() 
21
+    
22
+    with open("kakao_code.json", "w") as fp:
23
+        json.dump(tokens, fp) 
24
+    return tokens
25
+
26
+if __name__ == '__main__' :
27
+    app.run(debug = True)

+ 27
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_refresh_token.py Näytä tiedosto

@@ -0,0 +1,27 @@
1
+import requests
2
+import json
3
+
4
+rest_api_key = "1fd8d72c840797ca81faeedb8abdfcfb"
5
+
6
+def send_talk(token, text): 
7
+    header = {'Authorization': 'Bearer ' + token} 
8
+    url = 'https://kapi.kakao.com/v2/api/talk/memo/default/send' 
9
+    post = { 'object_type': 'text', 'text': text, 'link': { 'web_url': 'https://developers.kakao.com', 'mobile_web_url': 'https://developers.kakao.com' }, 'button_title': '키워드' } 
10
+    data = {'template_object': json.dumps(post)} 
11
+    return requests.post(url, headers=header, data=data) 
12
+
13
+if __name__ == '__main__' :
14
+    with open("kakao_code.json", "r") as fp:
15
+        ts = json.load(fp) 
16
+    r_token = ts["refresh_token"] 
17
+    
18
+    url = 'https://kauth.kakao.com/oauth/token' 
19
+    data = { "grant_type": "refresh_token", "client_id": rest_api_key, "refresh_token": r_token }
20
+    response = requests.post(url, data=data)
21
+    tokens = response.json() 
22
+    
23
+    with open("kakao_code.json", "w") as fp:
24
+        json.dump(tokens, fp) 
25
+    token = tokens["access_token"] 
26
+    send_talk(token, '보낼 메시지') 
27
+    

+ 15
- 0
src/kr/co/swh/lecture/python/scene5/kakao/kakao_send.py Näytä tiedosto

@@ -0,0 +1,15 @@
1
+import requests
2
+import json
3
+
4
+def send_talk(token, text): 
5
+    header = {'Authorization': 'Bearer ' + token} 
6
+    url = 'https://kapi.kakao.com/v2/api/talk/memo/default/send' 
7
+    post = { 'object_type': 'text', 'text': text, 'link': { 'web_url': 'https://developers.kakao.com', 'mobile_web_url': 'https://developers.kakao.com' }, 'button_title': '키워드' } 
8
+    data = {'template_object': json.dumps(post)} 
9
+    return requests.post(url, headers=header, data=data) 
10
+
11
+if __name__ == '__main__' :
12
+    with open("kakao_code.json", "r") as fp:
13
+        ts = json.load(fp) 
14
+    token = ts["access_token"] 
15
+    send_talk(token, '보낼 메시지') 

src/kr/co/swh/lecture/python/scene5/kakao/message_send_to_me.py → src/kr/co/swh/lecture/python/scene5/kakao/kakao_send_template.py Näytä tiedosto

@@ -7,8 +7,9 @@ import requests
7 7
 default_talk_url = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
8 8
 custom_talk_url = "https://kapi.kakao.com/v2/api/talk/memo/send"
9 9
 
10
-# token = "305sfgmbszYwrTCl6ytOo40kAMqmRcdc4gGVZwo9dZsAAAFzPCnqgQ"
11
-token = "ndRMXUzFVkjEDXikm7bDmRhW0yL8DpCl54uSKQo9dZwAAAFzPDj8Cg"
10
+with open("kakao_code.json", "r") as fp:
11
+    ts = json.load(fp) 
12
+token = ts["access_token"] 
12 13
 # header = {
13 14
 #     "Authorization": "Bearer {본인의 사용자 토큰 넣기}".format(
14 15
 #         token=token