C# 으로 나에게 메시지 보내기를 REST API 로 구현하는데,
아무리 해도 안됩니다. 계속 403 에러가 납니다.
//메시지 보내기
public void sendKakaoMessage(string message)
{
try
{
string apiURL = DefineKakao.HOST_API_URL + DefineKakao.KAKAO_MSG_SEND_API;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(apiURL);
req.ContentType = “application/x-www-form-urlencoded”;
//req.Headers.Add("ContentType", "application/x-www-form-urlencoded");
req.Headers.Add("Authorization", "Bearer " + DefineKakao.ACCESS_TOKEN);
req.Method = "POST";
req.KeepAlive = true;
JObject send = new JObject();
JObject link = new JObject();
send.Add("object_type", "text");
send.Add("text", message);
link.Add("web_url", "https://developers.kakao.com");
link.Add("mobile_web_url", "https://developers.kakao.com");
send.Add("link", link);
send.Add("button_title", "테스트");
string json = "template_object=" + send;
byte[] sendData = UTF8Encoding.UTF8.GetBytes(json);
req.ContentLength = sendData.Length;
Stream aaa = req.GetRequestStream();
aaa.Write(sendData, 0, sendData.Length);
aaa.Flush();
aaa.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)req.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string retValue = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
}
catch (Exception ex)
{
}
}
//토큰 및 권한 생성
private string getToken()
{
string token = string.Empty;
try
{
//https://kauth.kakao.com/oauth/authorize?client_id={REST_API_KEY}&redirect_uri={REDIRECT_URI}&response_type=code&scope=account_email
var clientAuth = new RestClient(DefineKakao.HOST_OAUTH_URL);
var requestAuth = new RestRequest("oauth/authorize", Method.GET);
requestAuth.AddParameter("client_id", DefineKakao.API_KEY);
requestAuth.AddParameter("redirect_uri", DefineKakao.REDIRECT_URL);
requestAuth.AddParameter("response_type", "code");
requestAuth.AddParameter("scope", "talk_message");
var resultAuth = clientAuth.Execute(requestAuth);
var client = new RestClient(DefineKakao.HOST_OAUTH_URL);
var request = new RestRequest("/oauth/token", Method.POST);
request.AddParameter("grant_type", "authorization_code");
request.AddParameter("client_id", DefineKakao.API_KEY);
request.AddParameter("redirect_uri", DefineKakao.REDIRECT_URL);
request.AddParameter("code", DefineKakao.USER_CODE);
//request.AddParameter("scope", "talk_message");
var result = client.Execute(request);
var json = JObject.Parse(result.Content);
return json["access_token"].ToString();
}
catch (Exception ex)
{
}
return token;
}
이렇게 구현을 했는데, 계속 에러가 나네요…
한번이라도 보내고 싶습니다.
여러분의 도움이 절실합니다.