C#.net代码如下:
return await Senparc.Weixin.MP.ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
{
var urlFormat = string.Format("https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token={0}", accessToken.AsUrlData());
var result = await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(null, urlFormat, data, CommonJsonSendType.POST, timeOut: timeOut);
return result;
}, accessTokenOrAppId);
这个创建直播间接口,不能用微信组件post过去吗???
当前时间是2020-08-13 13:03:00
直播开始时间 2020-08-13 15:00:00
直播结束时间 2020-08-13 16:00:00
调试情况(图片不清晰请放大看):
错误日志如下:
微信 POST 请求发生错误!错误代码:200002,说明:parameter startTime or endTime is invalid rid: 5f34c975-207cd6f4-0a61eec7
Senparc.Weixin.Exceptions.ErrorJsonResultException: 微信 POST 请求发生错误!错误代码:200002,说明:parameter startTime or endTime is invalid rid: 5f34c975-207cd6f4-0a61eec7
在 Senparc.Weixin.CommonAPIs.CommonJsonSend.<>c.<.cctor>b__6_1(String apiUrl, String returnText)
在 Senparc.CO2NET.HttpUtility.Post.
--- End of stack trace from previous location where exception was thrown ---
有遇上同样问题的吗?我这里卡住很久了。。。 欢迎一起讨论
----------------------已解决------------------------------
检查了微信封装的组件Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync,提交数据时默认Header中的ContentType是text/xml,需要自己另外封装接口 request.ContentType =“application/json; charset=UTF-8”
上代码:
public static string PostTest(string url, object data)
{
Encoding encoding = Encoding.UTF8;
var reqJson = JsonConvert.SerializeObject(data,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss" });
byte[] buffer = encoding.GetBytes(reqJson);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Post";
request.ContentType = "application/json; charset=UTF-8";
request.ContentLength = buffer.Length;
request.Timeout = 60000;
HttpWebResponse response = null;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
response = (HttpWebResponse) request.GetResponse();
using (StreamReader reader=new StreamReader(response.GetResponseStream(), encoding))
{
var reaponseString = reader.ReadToEnd();
response.Close();
return reaponseString;
}
}
DateTime dtNow = DateTime.Now;
int startTime = WXHelper.ConvertToTimeStamp(dtNow.AddHours(1));
int endTime = WXHelper.ConvertToTimeStamp(dtNow.AddHours(2));
var o = new
{
name = "测试直播房间1",
coverImg = "mIZ6J141w3OWw5sOBwG3NRM06YzG_hD7O4vsKVi85Y3vSvcKEy7C_i6Rd_Q8y4YD",
startTime = WXHelper.ConvertToTimeStamp(dtNow.AddMinutes(15)),
endTime = WXHelper.ConvertToTimeStamp(dtNow.AddHours(1)),
anchorName = "测试直播Name",
anchorWechat = "JnnJinn",
subAnchorWechat = "",
shareImg = "",
feedsImg = "",
isFeedsPublic = "1",
type = "1",
screenType = "1",
closeLike = "1",
closeGoods = "1",
closeComment = "1",
closeReplay = "1",
closeShare = "1",
closeKf = "1",
};
var token = Wechat.GetAccessToken(WechatAppType.MP, false);
var url = string.Format("https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token={0}", token);
{
"Result": true,
"Info": "{\"errmsg\":\"parameter startTime or endTime is invalid rid: 5f7e9943-69731824-18832adc\",\"errcode\":200002}",
"CreateDt": "2020-10-08 12:44:53",
"Data": {
"name": "测试直播房间1",
"coverImg": "mIZ6J141w3OWw5sOBwG3NRM06YzG_hD7O4vsKVi85Y3vSvcKEy7C_i6Rd_Q8y4YD",
"startTime": 1602104393,
"endTime": 1602107093,
"anchorName": "测试直播Name",
"anchorWechat": "JnnJinn",
"subAnchorWechat": "",
"shareImg": "",
"feedsImg": "",
"isFeedsPublic": "1",
"type": "1",
"screenType": "1",
"closeLike": "1",
"closeGoods": "1",
"closeComment": "1",
"closeReplay": "1",
"closeShare": "1",
"closeKf": "1"
}
}
很明显 参数 开始时间 格式不对 我找一下 我以前写的
//转换为时间戳
string sTime = Utils.getTimestamp(Utils.ObjectToDateTime(model.startTime));
string eTime = Utils.getTimestamp(Utils.ObjectToDateTime(model.endTime));
另外传参数的时候,加上“”
请告知如下错误如何处理。怎么修改都不行