简体   繁体   English

将cURL代码转换为C#Izooto API

[英]Converting cURL code to c# izooto api

I am trying to access an api for push notifications. 我正在尝试访问API的推送通知。

This is the cURL code: 这是cURL代码:

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"

I tried to access the api through c#. 我试图通过C#访问api。

C# Code: 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;
        }
    }

I am getting the following error when reaches WebResponse tResponse = tRequest.GetResponse(); 到达WebResponse tResponse = tRequest.GetResponse();时出现以下错误WebResponse tResponse = tRequest.GetResponse();

Error: System.Net.WebException: The remote server returned an error: (400) Bad Request. 错误: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Response : {"success":false,"message":"Authentication token missing"} 响应: {"success":false,"message":"Authentication token missing"}

I referred the following documentation for the curl code https://docs.izooto.com/docs/push-to-all Can anyone provide the solution for this. 我将以下文档用于卷曲代码https://docs.izooto.com/docs/push-to-all任何人都可以为此提供解决方案。 Thanks in advance. 提前致谢。

在postData中传递目标。

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  }";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM