Bot

c# 메시지 Bot 오류 문의

박재흥

2021.04.05읽음 4515

작성하신 내용은 누구나 볼 수 있습니다.

계정 이름, 도메인 이름, API ID, ConsumerKey 등의 비공개 정보는 기입하지마세요.

안녕하세요

메세지 Bot 으로 알람을 보낼려고 

C# 에서 REST API을 이용하여 코딩을 했습니다.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "POST";
request.Headers.Add("consumerKey", Consumer Key);
request.Headers.Add("Authorization", "Bearer Server List(ID 등록 방식)");
request.ContentType = "application/json;charset=UTF-8";
string inMsg = 
{
"accountId": 네이버웍스 ID,
"content":{
   "type":"text",
   "text":"hello"
           }
};
{
     byte[] bytes = UTF8Encoding.UTF8.GetBytes(inMsg);
     request.ContentLength = bytes.Length;
     Stream os = request.GetRequestStream();
     os.Write(bytes, 0, bytes.Length);
     os.Close();
}
           string outMsg;
           {
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               int stsCode = (int)response.StatusCode;
               if (stsCode != 200)
               {
                   string stsDesc = response.StatusDescription;
                   throw new Exception(stsCode + ": " + stsDesc);
               }
               Stream stream = response.GetResponseStream();
               StreamReader reader = new StreamReader(stream/*, UTF8Encoding.UTF8*/);
               outMsg = reader.ReadToEnd();
               string prefix = outMsg.Substring(0, 50);
               if (prefix.Contains("\"success\":false") || prefix.Contains("\"success\" : false"))
               {
                   throw new Exception(outMsg.Length < 300 ? outMsg : outMsg.Substring(0, 300));
               }
           }
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 이부분에서 에러가 납니다
원격 서버에서 (401) 권한이 없음 오류를 반환했습니다.
권한이 없다고 에러가 나는데 무엇이 문제 일까요?

댓글1

  • 업데이트 된 답글입니다.

    branches

    401에러라면 함께 리턴되는 메시지를 확인해 보세요~ 인증에서 걸린것 같은데 아마 셋중에 하나일 겁니다.

    Authentication failed : 컨슈머키와 토큰의 인증 실패
    Authentication header not exists : 컨슈머키와 토큰이 헤더에 없음
    Malformed authentication header : 헤더의 컨슈머키와 토큰 형식이 틀림

    혹시 Authorization 값을 "Bearer 토큰값"으로 잘 설정하셨는지도 확인해 보세요
    먼저 인증이해결되어야 메시지전송 API가 실행되거든요

    2021.04.05

    0
이전 글드라이브 API 관련 문의 입니다.
다음 글고객/거래처 연락처 목록 조회 API 관련 문의드립니다.
목록

궁금한 점을 해결하지 못하셨나요?
지금 바로 NAVER WORKS 사용자들에게 물어보세요!