小程序app.js
wx.login({
success:res=>{
console.log(res.code);
}
})
用以上的res.code通过服务端获取openid报{“errcode”:41008,“errmsg”:“missing code, hints: [ req_id: rn6PNA0941hc52 ]”}
以下是c#的服务端代码
public string GetOpenIdByCode(string code)
{
string html = string.Empty;
string url = “https://api.weixin.qq.com/sns/jscode2session?appid=XXX&secret=XXX&code=” + code + “&grant_type=authorization_code”;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = “GET”;
request.ContentType = “text/html;charset=UTF-8”;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream ioStream = response.GetResponseStream();
StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);
html = sr.ReadToEnd();
sr.Close();
ioStream.Close();
response.Close();
return html;
}