1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Php公众号40029 微信开发之微信公众平台 网页授权及 40029 问题解决

Php公众号40029 微信开发之微信公众平台 网页授权及 40029 问题解决

时间:2018-08-11 01:23:32

相关推荐

Php公众号40029 微信开发之微信公众平台 网页授权及 40029 问题解决

本文将带你了解微信开发微信公众平台,网页授权及 40029 问题解决,希望本文对大家学微信有所帮助。

1、跳转授权链接

https://open./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':'invalidcode,hints:[req_id:0407ns44]'} 这个错误,调试时发现会访问两次,一次是301,页面重定向了,第二次跟第一次就差这个参数。加上后,不再报错。code 可以了。

二需要注意的是redirect_uri = xxx 需要自己URL转码

2、获取用户信息,直接上代码。

using System;using System.Web;using Newtonsoft.Json.Linq;using ;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; } /// /// 用code 获取 Token /// private string GetToken(string _code) { if (_code.IndexOf("\"") > -1) _code = _code.Replace("\"", ""); string URL = "https://api./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; } /// /// 获取用户信息 /// 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./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; } /// /// 刷新 Token /// private string RefreshToken(string refresh_token) { if (refresh_token.IndexOf("\"") > -1) refresh_token = refresh_token.Replace("\"", ""); string R_URL = "https://api./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; } /// /// 访问地址,并获取数据 /// 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"; .WebResponse response = hr.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); string ReturnVal = reader.ReadToEnd(); reader.Close(); response.Close(); return ReturnVal; }}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。