![](/img/trans.png)
[英]“Specified value has invalid HTTP Header characters. Parameter name: name”
[英]Specified value has invalid HTTP Header characters
我試圖從其ASP.NET站點通過其REST API訪問WOWZA流雲,但是對RESTSharp經驗很少。
這是我嘗試創建的curl的示例:
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,\\”代碼\\“:\\” ERR-401-InvalidApiKey \\“,\\”標題\\“:\\”無效的API密鑰錯誤\\“,\\” message \\“:\\”無效的API密鑰。\\“,\\”描述\\“:\\” \\“,\\”鏈接\\“:[]}}”
您確定要添加“內容類型”,“ 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"]);
你的頭,你不要錯誤車道需要重復的內容類型的值,並移除:
從API鍵名:
newRequest.AddParameter("content-type", "application/json");
newRequest.AddParameter("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
在OP更新問題時更新
因此,現在修改之后,該呼叫通過並正常運行。 當前錯誤與您傳遞的無效api密鑰有關-可能是您傳遞的訪問密鑰/ api密鑰已被嵌入/過期。
感謝大家的幫助-我現在可以通過C#包裝的REST API和restsharp與Wowza Cloud Streaming無縫交互。 最后的解決方案是使用Visual Studio函數通過選擇性粘貼生成生成模板JSON類(請參閱在C#中,如何為具有多個嵌套數組的JSON對象建模? )-生成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);
事實證明,使用的URL是錯誤的-在文檔中沒有特別說明,但是對於沙盒使用,URL是https://api-sandbox.cloud.wowza.com 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.