GCP/Apps Script
Apps Script로 카카오톡 메세지 보내기 위해 만료된 토큰 REFRESH 하기
whistory
2023. 6. 2. 09:39
반응형
메세지 보내기를 기능을 완료하고,
다음날 카카오톡 메세지를 전송해보았다.
발급받았던 Access token이 만료되었다.
아 여기있던 refresh token을 사용할 때 이구나!
{
"access_token":"메세지 전송에 사용될 KEY"
, "token_type":"bearer"
, "refresh_token":"리프레시 토큰"
, "expires_in":9999
, "scope":"talk_message"
, "refresh_token_expires_in":9999
}
// STEP #1 - portal rest api key
const CLIENT_ID = "포탈에서 받은 REST API KEY";
const REFRESH_TOKEN = "생성한 REFRESH TOKEN";
function getAuthRefresh() {
const url = "https://kauth.kakao.com/oauth/token";
data = {
"grant_type" : "refresh_token",
"client_id" : CLIENT_ID,
"redirect_uri" : "https://whiseung.tistory.com",
"refresh_token": REFRESH_TOKEN
}
const options = {
contentType: "application/x-www-form-urlencoded;charset=utf-8",
headers: {
Authorization: "Bearer " + CLIENT_ID
},
payload: data
};
const res = UrlFetchApp.fetch(url, options).getContentText();
const returndData = JSON.parse(res);
return returndData.access_token;
}
function sendMessageToMe() {
const NEW_TOKEN = getAuthRefresh();
const url = "https://kapi.kakao.com/v2/api/talk/memo/default/send";
var dataString = `template_object={
"object_type": "text",
"text": " refresh!",
"link": {
"web_url": "https://developers.kakao.com",
"mobile_web_url": "https://developers.kakao.com"
},
"button_title": "바로 확인"
}`;
const options = {
method: "POST",
headers: {
Authorization: "Bearer " + NEW_TOKEN
, contentType: "application/x-www-form-urlencoded;charset=utf-8"
},
payload: dataString
};
const res = UrlFetchApp.fetch(url, options).getContentText();
console.log(res);
}
반응형