[微信] 客服接口调用的时候返回 40003 Invalid OpenID

首先确认收件人在24小时内主动向公众号发过消息。该消息的 FromUserId 即是客服消息的 touser 参数的 OpenId

2017-05-19 更新:可以使用UTF-8了

string url = $"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={token}";
using (var web = new WebClient())
{
    web.Encoding = Encoding.UTF8;
    var resultJson = web.UploadString(url, JsonConvert.SerializeObject(message));
    var result = JsonConvert.DeserializeObject<WeChatJsonResult>(resultJson);
    return result;
}

——以下内容不适用

其次,确认使用的编码是 ASCII ,UTF-8无效!!!干了一下午。终于找到方法了。

PostString 的测试样例:

request.Method = "POST";
request.ContentType = "application/json";    
request.Timeout = 5000;    // Five seconds time-out for a failed http connection
request.KeepAlive = false;
var requestStream = request.GetRequestStream();
using (TextWriter tw = new StreamWriter(requestStream, Encoding.ASCII))
{
  tw.Write(PostString);
}

我得到了 http://mp.weixin.qq.com/qa/index.php?qa=10715&qa_1=%E5%8F%91%E9%80%81%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF%E6%8F%90%E7%A4%BA-invalid-openid 页面中的启发。

原文地址:https://www.cnblogs.com/lionetchen/p/3954692.html