指定的值具有无效的HTTP标头字符

时间:2017-01-26 09:32:13

标签: c# asp.net restsharp

我试图通过他们的REST API从ASP.NET站点访问WOWZA Streaming Cloud,但是对于RESTSharp几乎没有经验。

以下是我尝试创建的示例:

curl -H 'wsc-api-key: KEY' -H 'wsc-access-key: KEY' -H 'Content-Type:
application/json' -X POST -d '{"live_stream": {"name": "MyNewLiveStream",
"transcoder_type": "transcoded", "billing_mode": "pay_as_you_go",
"broadcast_location": "us_west_california", "encoder": "other_rtmp",
"delivery_method": "push", "aspect_ratio_width": 1920,
"aspect_ratio_height": 1080}}'https://api.cloud.wowza.com/api/v1/live_streams/

请参阅:https://sandbox.cloud.wowza.com/apidocs/v1/

以下是使用的c#代码:

var client = new RestClient("https://api.cloud.wowza.com/");
        var newRequest = new RestRequest("api/v1/live_streams",Method.POST);
        newRequest.AddHeader("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
        newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);
        newRequest.AddHeader("Content-Type", "application/json");
        var body = "{\"live_stream\": {" +
            "\"aspect_ratio_height\": 720," +
            "\"aspect_ratio_width\": 1280," +
            "\"billing_mode\": \"pay_as_you_go\"," +
            "\"broadcast_location\": \"us_west_california\"," +
            "\"closed_caption_type\": \"none\"," +
            "\"delivery_method\": \"push\"," +
            "\"encoder\": \"wowza_gocoder\"," +
            "\"hosted_page\": true," +
            "\"hosted_page_sharing_icons\": true," +
            "\"name\": \"MyLiveStream\"," +
            "\"player_countdown\": false," +
            "\"player_responsive\": true," +
            "\"player_type\": \"original_html5\"," +
            "\"player_width\": 0," +
            "\"recording\": false," +
            "\"target_delivery_protocol\": \"hls\"," +
            "\"transcoder_type\": \"transcoded\"," +
            "\"use_stream_source\": false}";

        newRequest.AddJsonBody(body);
        IRestResponse myResponse = client.Execute(newRequest);

根据回复进行修改后,响应代码现在是

  

" {\"元\":{\"状态\":401,\"代码\":\&# 34; ERR-401-InvalidApiKey \",\" title \":\"无效的Api键错误\","消息\&#34 ;:\" API密钥无效。\",\"说明\":\" \",\"链接\&#34 ;:[]}}"

3 个答案:

答案 0 :(得分:1)

您确定要添加“content-type”,“wsc-api-key”和“wsc-access-key”作为请求参数吗?

应将它们添加到请求标头中,如下所示。

   newRequest.AddHeader("content-type", "Content-Type: application/json");
   newRequest.AddHeader("wsc-api-key:", ConfigurationManager.AppSettings["WowzaAPIKey"]);
   newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);

答案 1 :(得分:1)

您的标头错误 - 您不需要重复内容类型值并从api-key名称中删除:

 newRequest.AddParameter("content-type", "application/json"); 
 newRequest.AddParameter("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);

更新OP,更新了问题

所以现在在修改之后,通话已经通过并且正常工作。当前错误是关于您传递的无效api密钥 - 可能是您传递的访问密钥/ api密钥是已激活/已过期。

答案 2 :(得分:1)

感谢大家的帮助 - 我现在可以通过C#包装的REST api和使用restsharp与Wowza Cloud Streaming无缝交互。 最后的解决方案是使用visual studio函数通过Paste Special生成模板JSON类(参见In C#, how do I model a JSON object with multiple nested arrays?) - 生成WowzaLivestream类。

然后在最后使用这段代码:

_restClient = new RestClient("https://api-sandbox.cloud.wowza.com");
var newRequest = new RestRequest("api/v1/live_streams", Method.POST);
newRequest.RequestFormat = DataFormat.Json;

        newRequest.AddHeader("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
        newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);
        newRequest.AddHeader("Content-Type", "application/json");
        newRequest.AddHeader("Accept", "application/json");

        var requestBody = new WowzaLivestream
        {
            live_stream = new Live_Stream
            {
                aspect_ratio_height = 1024,
                aspect_ratio_width = 1900,
                billing_mode = "pay_as_you_go",
                broadcast_location = "eu_ireland",
                closed_caption_type = "none",
                delivery_method = "push",
                encoder = "other_rtmp",
                name = "WowzaLStest2: " + DateTime.Now.ToShortTimeString(),
                player_countdown = true,
                player_responsive = true,
                player_type = "original_html5",
                player_width = 0,
                recording = false,
                target_delivery_protocol = "hls",
                transcoder_type = "transcoded",
                use_stream_source = false
            }
        };

        var json = newRequest.JsonSerializer.Serialize(requestBody);
        newRequest.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
        IRestResponse myResponse = _restClient.Execute(newRequest);

事实证明,使用的网址是错误的 - 它没有在文档中明确说明,但是对于沙箱使用,网址为https://api-sandbox.cloud.wowza.com