指定的值包含无效的 HTTP Header 个字符

Specified value has invalid HTTP Header characters

我正在尝试从 ASP.NET 站点通过他们的 REST API 访问 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);

根据响应进行修改后,响应代码现在为

"{\"meta\":{\"status\":401,\"code\":\"ERR-401-InvalidApiKey\",\"title\":\"Invalid Api Key Error\",\"message\":\"Invalid API key.\",\"description\":\"\",\"links\":[]}}"

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

它们应该添加到请求中 header 如下。

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

您的 headers 是错误的 - 您不需要在值中重复 content-type 并从 api-key 名称中删除 :

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

随着 OP 更新问题而更新

所以现在修改后,调用正常了。当前错误是关于您传递的无效 api 密钥 - 可能是您传递的访问 key/api 密钥是 invlaid/expired.

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

然后最后使用这段代码:

_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);

事实证明,使用的 URL 是错误的 - 文档中没有具体说明,但对于沙箱使用,URL 是 https://api-sandbox.cloud.wowza.com