KakaoDevelopers
lastmod 2025-02-15created 2022-05-2018 edits
How to use the Kakao API
Categories
RESTful API
Message API
- [Recommended] ★Sending a message to yourself using the Kakao API
Proceed as follows.
- Go to the Kakao Developers website ( https://developers.kakao.com/ ) and log in. (Firefox didn't work, Chrome did)
-
Top menu > My Application > Add an application- App name: [enter manually]
- Business name: [enter manually]
App Settings > Summary > REST API key <- Copy the key !! Kakao Login > Activation settings Kakao Login > Redirect URI and <- Copy the address !! It must match exactly.- Add the OAuth Redirect URI (copy)
Consent Items > Send KakaoTalk message <- Copy the ID (talk_message) !!- Select consent during use
- Enter the purpose of consent
- e.g. "Test alarm"
App Settings > Platform > Web- Add the website domain (do not add '/' at the end of the address)
To register OAuth, fill in the following address:
https://kauth.kakao.com/oauth/authorize?client_id={REST API 키}&redirect_uri={OAuth Recirect URL}&response_type=code&scope={동의항목 ID}
For example, if:
- REST API key:
e19ed0d304ec37d2be3ed041fd60e323 - OAuth Redirect URI:
https://localhost:20000/ - Consent item ID:
talk_message
then:
https://kauth.kakao.com/oauth/authorize?client_id=e19ed0d304ec37d2be3ed041fd60e323&redirect_uri=https://localhost:20000/&response_type=code&scope=talk_message
| WARNING |
| From here on, the code value keeps changing, so you must reconnect |
Open it in the browser's incognito mode and Accept. You will then be redirected to an address like the following.
Copy the part corresponding to "{코드}" (code) above.
For example:
- If the address is
https://localhost:20000/?code=BurXfeurZ2RJ4kM6r-6Y2z_-h3geKLmphRaysdso6VBIAkP-vkce7ZeomcJEElyNw3JIpAo3dRoAAAGC986kBw, - the code is
BurXfeurZ2RJ4kM6r-6Y2z_-h3geKLmphRaysdso6VBIAkP-vkce7ZeomcJEElyNw3JIpAo3dRoAAAGC986kBw.
After that, you must register the code from your personal server using the KakaoTalk message API.
A sample follows (copy {REST API 키} (REST API key), {OAuth Redirect URI}, and {코드} (code) from above into the sample below)
import requests
import json
import sys
# 카카오톡 메시지 API
url = "https://kauth.kakao.com/oauth/token"
rest_api_key = "e19ed0d304ec37d2be3ed041fd60e323"
oauth_redirect_uri = "https://localhost:20000/"
# code = "BurXfeurZ2RJ4kM6r-6Y2z_-h3geKLmphRaysdso6VBIAkP-vkce7ZeomcJEElyNw3JIpAo3dRoAAAGC986kBw"
code = sys.argv[1]
data = {
"grant_type" : "authorization_code",
"client_id" : rest_api_key,
"redirect_url" : oauth_redirect_uri,
"code" : code
}
response = requests.post(url, data=data)
tokens = response.json()
print(tokens) # 이 결과
with open("kakao_code.json", "w") as fp:
json.dump(tokens, fp)
The resulting Response is printed as follows.
{
'access_token': 'v247CUaLKUv6HMXsdnWWkuqJhmRI2jOT2fP1xn9eCj1y6gAAAYLn0ibe',
'token_type': 'bearer',
'refresh_token': 'f2k3GNKZPnt1bYZvVUmuz-HnELde1Kzjd9Z1iDNbCj9y6gAAAYLn0iwc',
'expires_in': 21599,
'scope': 'talk_message',
'refresh_token_expires_in': 5183999
}
Copy the access_token value above and add it to the Authorization header below.
import json
import requests
url = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
access_token = "v247CUaLKUv6HMXsdnWWkuqJhmRI2jOT2fP1xn9eCj1y6gAAAYLn0ibe"
headers = {"Authorization": "Bearer " + access_token}
data = {
"template_object" : json.dumps({ "object_type" : "text",
"text" : "Google 뉴스: drone",
"link" : {
"web_url" : "https://www.google.co.kr/search?q=drone&source=lnms&tbm=nws",
"mobile_web_url" : "https://www.google.co.kr/search?q=drone&source=lnms&tbm=nws"
}
})
}
response = requests.post(url, headers=headers, data=data)
if response.json().get('result_code') == 0:
print('메시지를 성공적으로 보냈습니다.')
else:
print('메시지를 성공적으로 보내지 못했습니다. 오류메시지 : ' + str(response.json()))
If you check KakaoTalk, you can see a chat has arrived from your own ID.
List message example
import json
import requests
url = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
headers = {"Authorization": "Bearer " + "qixkf7oJe3e9hWuLmKpUql9GKcMbZqEWupnrOidzCilv1QABAYDe2-fh"}
template = {
"object_type" : "list",
"header_title" : "Google",
"header_link" : {
"web_url" : "www.google.com",
"mobile_web_url" : "www.google.com"
},
"contents" : [
{
"title" : "1. 국립공원 뉴스",
"description" : "검색어: national park",
"image_url" : "https://cdn.kado.net/news/photo/201901/948844_399953_0825.jpg",
"image_width" : 50, "image_height" : 50,
"link" : {
"web_url" : "https://www.google.co.kr/search?q=national+park&source=lnms&tbm=nws",
"mobile_web_url" : "https://www.google.co.kr/search?q=national+park&source=lnms&tbm=nws"
}
},
{
"title" : "2. 딥러닝 뉴스",
"description" : "검색어: deep learning",
"image_url" : "https://cdn-images-1.medium.com/max/1200/1*iDQvKoz7gGHc6YXqvqWWZQ.png",
"image_width" : 50, "image_height" : 50,
"link" : {
"web_url" : "https://www.google.co.kr/search?q=deep+learning&source=lnms&tbm=nws",
"mobile_web_url" : "https://www.google.co.kr/search?q=deep+learning&source=lnms&tbm=nws"
}
}
],
"buttons" : [
{
"title" : "Google로 이동",
"link" : {
"web_url" : "www.google.com",
"mobile_web_url" : "www.google.com"
}
}
]
}
data = {
"template_object" : json.dumps(template)
}
response = requests.post(url, data=data, headers=headers)
# print(response.status_code)
if response.json().get('result_code') == 0:
print('메시지를 성공적으로 보냈습니다.')
else:
print('메시지를 성공적으로 보내지 못했습니다. 오류메시지 : ' + str(response.json()))
See also
- PyKakao - Python package for the Kakao API