微信公众平台,网页授权及 40029 问题解决
1、跳转授权链接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=xxx&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect
&connect_redirect=1 这个参数,刚开始没有加,总是报 {'errcode':40029,'errmsg':'invalid code, hints: [ req_id: 0407ns44 ]'} 这个错误 ,调试时发现会访问两次,一次是301,页面重定向了,第二次跟第一次就差这个参数。加上后,不再报错。code 可以了。
二需要注意的是 redirect_uri = xxx 需要自己URL转码
2、获取用户信息,直接上代码。
<%@ WebHandler Language="C#" Class="getinfos" %> using System; using System.Web; using Newtonsoft.Json.Linq; using System.Net; using System.IO; using System.Text; public class getinfos : IHttpHandler { public void ProcessRequest (HttpContext context) { //一切都从这里开始 string code = context.Request["code"].ToString(); context.Response.ContentType = "text/plain"; context.Response.Write(GetInfos(code)); } public bool IsReusable { get { return false; } } public string GetInfos(string _code) { // string _access_tokens = GetToken(_code); JObject json = JObject.Parse(_access_tokens); string access_token = json["access_token"].ToString(); string refresh_token = json["refresh_token"].ToString(); string openid = json["openid"].ToString(); // string _infos = GetUserInfo(access_token, openid); JObject info = JObject.Parse(_infos); if (info["errcode"] != null) { _access_tokens = RefreshToken(refresh_token); json = JObject.Parse(_access_tokens); access_token = json["access_token"].ToString(); openid = json["openid"].ToString(); _infos = GetUserInfo(access_token, openid); } return _infos; } /// <summary> /// 用code 获取 Token /// </summary> private string GetToken(string _code) { if (_code.IndexOf("\"") > -1) _code = _code.Replace("\"", ""); string URL = "https://api.weixin.qq.com/sns/oauth2/access_token"; string RequestPara = "?appid=xxx"; RequestPara += "&secret=xxx"; RequestPara += "&code=" + _code; RequestPara += "&grant_type=authorization_code"; string _access_tokens = GetData(URL, RequestPara); _access_tokens = _access_tokens.Replace("\"", "'"); return _access_tokens; } /// <summary> /// 获取用户信息 /// </summary> private string GetUserInfo(string access_token, string openid) { if (access_token.IndexOf("\"") > -1) access_token = access_token.Replace("\"", ""); if (openid.IndexOf("\"") > -1) openid = openid.Replace("\"", ""); string I_URL = "https://api.weixin.qq.com/sns/userinfo"; string I_RequestPara = "?access_token=" + access_token; I_RequestPara += "&openid=" + openid; I_RequestPara += "&lang=zh_CN"; string _infos = GetData(I_URL, I_RequestPara); _infos = _infos.Replace("\"", "'"); return _infos; } /// <summary> /// 刷新 Token /// </summary> private string RefreshToken(string refresh_token) { if (refresh_token.IndexOf("\"") > -1) refresh_token = refresh_token.Replace("\"", ""); string R_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token"; string R_RequestPara = "?appid=xxx"; R_RequestPara += "&grant_type=refresh_token"; R_RequestPara += "&refresh_token=" + refresh_token; string _infos = GetData(R_URL, R_RequestPara); _infos = _infos.Replace("\"", "'"); return _infos; } /// <summary> /// 访问地址,并获取数据 /// </summary> private string GetData(string URL, string RequestPara) { RequestPara = RequestPara.IndexOf('?') > -1 ? (RequestPara) : ("?" + RequestPara); WebRequest hr = HttpWebRequest.Create(URL + RequestPara); byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara); hr.Method = "GET"; System.Net.WebResponse response = hr.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); string ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); return ReturnVal; } }