大家好,又见面了,我是你们的朋友全栈君。
本文将带你了解微信开发微信公众平台,网页授权及 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、获取用户信息,直接上代码。
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; } /// /// 用code 获取 Token /// 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; } /// /// 获取用户信息 /// 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; } /// /// 刷新 Token /// 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; } /// /// 访问地址,并获取数据 /// 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; }}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131477.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...