2017-12-27 24 views
1

푸시 알림 용 API에 액세스하려고합니다.cURL 코드를 C# izooto api로 변환

이는 컬 코드는 다음과 같습니다

curl -X POST \ 
    -H "Authentication-Token: {API_TOKEN}" \ 
    -H "Content-Type: application/json" \ 
    -d '{ 
    "title" : "{NOTIFICATION_TITLE}", 
    "message" : "{NOTIFICATION_MESSAGE}", 
    "icon_url" : "{ICON_URL}", 
    "banner_url" : "{BANNER_URL}", 
    "landing_url" : "{LANDING_URL}", 
    "actions" : [ 
     { 
     "text" : "{BUTTON1_TEXT}", 
     "url" : "{BUTTON1_URL}" 
     }, 
     { 
     "text" : "{BUTTON2_TEXT}", 
     "url" : "{BUTTON2_URL}" 
     }], 
    "utm_source" : "{UTM_SOURCE}", 
    "utm_medium" : "{UTM_MEDIUM}", 
    "utm_campaign" : "{UTM_CAMPAIGN}", 
    "ttl" : {TTL_SECONDS}, 
    "target" : { 
      "type" : "all" 
    } 
    }' "https://apis.izooto.com/v1/notifications" 

내가 C#을 통해 API에 액세스했습니다.

C# 코드 :

public string get() 
    { 
     try 
     { 
      WebRequest tRequest; 
      tRequest = WebRequest.Create("https://apis.izooto.com/v1/notifications"); 
      tRequest.Method = "post"; 
      tRequest.ContentType = "multipart/form-data"; 
      tRequest.Headers.Add("Authentication-Token", "xxxxxxxxx-yyyyyyyyy"); 

      string imgurl = "https://cdnimg.izooto.com/9338/9883/93381513921358.png"; 
      string landing_url = "http://www.maalaimalar.com/News/TopNews/2017/12/27110835/1136906/MK-Stalin-Slams-his-Brother-MK-Stalin-for-RK-Nagar.vpf"; 
      string postData = "title=test&message=testmsg&icon_url=" + imgurl + "&landing_url=" + landing_url + ""; 

      Console.WriteLine(postData); 

      Byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      tRequest.ContentLength = byteArray.Length; 
      Stream dataStream = tRequest.GetRequestStream(); 

      dataStream.Write(byteArray, 0, byteArray.Length); 
      dataStream.Close(); 
      WebResponse tResponse = tRequest.GetResponse(); 

      dataStream = tResponse.GetResponseStream(); 
      StreamReader tReader = new StreamReader(dataStream); 
      String sResponseFromServer = tReader.ReadToEnd(); 

      //lblStat.Text = sResponseFromServer; 
      tReader.Close(); 
      dataStream.Close(); 
      tResponse.Close(); 
      return sResponseFromServer; 
     } 
     catch (Exception e) 
     { 
      return e.Message; 
     } 
    } 

에 도달 할 때 다음과 같은 오류를 얻고있다 WebResponse tResponse = tRequest.GetResponse();

오류 : System.Net.WebException: The remote server returned an error: (400) Bad Request.

응답 :

{"success":false,"message":"Authentication token missing"} 나는 컬 코드에 대해 다음 문서를 참조 https://docs.izooto.com/docs/push-to-all 누구나이 솔루션을 제공 할 수 있습니까? 미리 감사드립니다. 이 JSON을 원하지만 당신은 양식 데이터를 보내는처럼 아래 postData.Like에서

+0

나에게 보인다. – Crowcoder

+0

"application/json"도 시도했습니다 –

+0

콘텐츠 유형 헤더를 변경할 수 없으며 POST Body에 유효한 JSON을 제공해야합니다. – Crowcoder

답변

2

패스 대상,

string postData= "{\n \"title\" : \""+ title + "\",\n \"message\" : \""+ message + "\",\n \"icon_url\" : \""+ icon_url + "\",\n \"banner_url\" : \"" + bannerUrl + "\",\n \"landing_url\" : \"" + landing_url + "\",\n \"target\" : {\n   \"type\" : \"all\"\n }\n }";