Post RestSharp Google API 错误
Post RestSharp Google API Error
我正在尝试使用 RestSharp 访问 YouTube API。
发布时出现错误 code:400,原因如下:
"This API does not support parsing form-encoded input"。
下面是我的代码的一小段:
var client = new RestClient("https://www.googleapis.com");
var request = new RestRequest(Method.POST);
request.Resource = "youtube/v3/liveBroadcasts";
request.RequestFormat = DataFormat.Json;
request.AddParameter("part", "snippet,status");
request.AddParameter("key", "MyClientId");
request.AddHeader("Authorization", "Bearer " + "MyAccessCode");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddBody(aJson);
try
{
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
上述回复内容为"This API does not support parsing form-encoded input"
在我发送的 Json 中 (aJson) 看起来像这样:
{
"snippet": {
"scheduledEndTime": "2015-01-10T12:11:11.0+0400",
"scheduledStartTime": "2015-01-10T11:11:11.0+0400",
"title": "MyBroadcastName"
},
"kind": "youtube#liveBroadcast",
"status": {
"privacyStatus": "private"
}
}
我很乐意获得与上述请求相关的任何帮助。
我做错了什么?
谢谢,
R.
问题出在您对 request.AddParameter
的调用上。您希望那些附加 URL 作为查询参数,但是当 HTTP 方法是 POST 时,RestSharp 默认将它们作为 URL 编码的表单数据发送。你想要这样的东西:
request.Resource = "youtube/v3/liveBroadcasts?part={part}&key={key}";
request.AddParameter("part", "snippet,status", ParameterType.UrlSegment);
request.AddParameter("key", "MyClientId", ParameterType.UrlSegment);
感谢托德,经过一些额外的挖掘,我发现了问题。
下面是固定代码:
var client = new RestClient("https://www.googleapis.com");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.Resource = "youtube/v3/liveBroadcasts?part={part}&key={key}";
request.AddParameter("part", "snippet,status", ParameterType.UrlSegment);
request.AddParameter("key", "MyClientId", ParameterType.UrlSegment);
request.AddHeader("Authorization", "Bearer " + "MyAccessCode");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddBody(anObject); //<== Here you should use an object and NOT a json. RestSharp will do the serialization!
try
{
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
感谢您的帮助。
R.
我正在尝试使用 RestSharp 访问 YouTube API。 发布时出现错误 code:400,原因如下: "This API does not support parsing form-encoded input"。 下面是我的代码的一小段:
var client = new RestClient("https://www.googleapis.com");
var request = new RestRequest(Method.POST);
request.Resource = "youtube/v3/liveBroadcasts";
request.RequestFormat = DataFormat.Json;
request.AddParameter("part", "snippet,status");
request.AddParameter("key", "MyClientId");
request.AddHeader("Authorization", "Bearer " + "MyAccessCode");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddBody(aJson);
try
{
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
上述回复内容为"This API does not support parsing form-encoded input" 在我发送的 Json 中 (aJson) 看起来像这样:
{
"snippet": {
"scheduledEndTime": "2015-01-10T12:11:11.0+0400",
"scheduledStartTime": "2015-01-10T11:11:11.0+0400",
"title": "MyBroadcastName"
},
"kind": "youtube#liveBroadcast",
"status": {
"privacyStatus": "private"
}
}
我很乐意获得与上述请求相关的任何帮助。 我做错了什么?
谢谢, R.
问题出在您对 request.AddParameter
的调用上。您希望那些附加 URL 作为查询参数,但是当 HTTP 方法是 POST 时,RestSharp 默认将它们作为 URL 编码的表单数据发送。你想要这样的东西:
request.Resource = "youtube/v3/liveBroadcasts?part={part}&key={key}";
request.AddParameter("part", "snippet,status", ParameterType.UrlSegment);
request.AddParameter("key", "MyClientId", ParameterType.UrlSegment);
感谢托德,经过一些额外的挖掘,我发现了问题。 下面是固定代码:
var client = new RestClient("https://www.googleapis.com");
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.Resource = "youtube/v3/liveBroadcasts?part={part}&key={key}";
request.AddParameter("part", "snippet,status", ParameterType.UrlSegment);
request.AddParameter("key", "MyClientId", ParameterType.UrlSegment);
request.AddHeader("Authorization", "Bearer " + "MyAccessCode");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddBody(anObject); //<== Here you should use an object and NOT a json. RestSharp will do the serialization!
try
{
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
感谢您的帮助。 R.