import requests
import urllib
import json

def getAccessToken(refreshToken) :
    url = "https://kauth.kakao.com/oauth/token"
    payload = "grant_type=refresh_token&client_id=1fd8d72c840797ca81faeedb8abdfcfb&refresh_token=" + refreshToken
    headers = {
        'Content-Type' : "application/x-www-form-urlencoded",
        'Cache-Control' : "no-cache",
    }

    reponse = requests.request("POST",url,data=payload, headers=headers)
    access_token = json.loads(((reponse.text).encode('utf-8')))
    return access_token

def sendText(accessToken) :
    url = 'https://kapi.kakao.com/v2/api/talk/memo/default/send'
    payloadDict = dict({
            "object_type" : "text",
            "text" :"sample text",
            "link" : {
                "web_url" : "http://localhost:5000",
                "mobile_web_url" : "http://localhost:5000.mobile"
             },
            "button_title" : "button-title",
            })
#     payload = 'template_object=' + str(json.dumps(payloadDict))
    payload = 'template_object=' + str(payloadDict)
    print(payload)
    headers = {
        'Content-Type' : "application/x-www-form-urlencoded",
        'Cache-Control' : "no-cache",
        'Authorization' : "Bearer " + accessToken,
    }

    reponse = requests.request("POST",url,data=payload, headers=headers)
    access_token = json.loads(((reponse.text).encode('utf-8')))
    return access_token

result =  getAccessToken("LZDlINbTkVDL-kiJoS_o0nyOPW25MqdhHQaBMgorDKcAAAFzHZWGXg")   # 메세지 받을 사람의 REFRESH TOKEN 이용
print(result['access_token'])
print(sendText(result['access_token']))