• 热线电话
  • 17798885678
  • 18021659625
新闻资讯
联系我们

 

南通米锐软件工作室

业务手机:18021659625

企业邮箱:mirocn@163.com

地址:南通市如皋市中山东路210号东景国际

uniapp调用后台C#生成带参数小程序码

日期:2022/6/24 15:58:23
    网上找了很多代码实际运行时报各种错,这里总结一下调试过程中遇到的报错:
1、access_token无效:
 
错误代码:{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest rid: 62b51859-648c2b4c-7722f316"}
分析原因:GetAccessToken返回的是JSON串,需要使用JObject tt =(JObject)JsonConvert.DeserializeObject(token);转换一下。
 
2、传入参数无效或不是JSON格式
 
错误代码:{"errcode":40169,"errmsg":"invalid length for scene, or the data is not json string rid: 62b53000-09ef0c32-425ac4f4"}
或:{"errcode":40129,"errmsg":"invalid scene rid: 62b54a10-06d2490b-1b4e6fc3"}
 
分析原因:参数格式必须是id=xxx这样的格式,不能是空或都其它格式。
 
经实测可正常运行的DEMO代码如下:
 
        /// <summary>
        /// 获取TOKEN
        /// </summary>
        /// <param name="strAPPID"></param>
        /// <param name="strSecret"></param>
        /// <returns></returns>
        public static string GetAccessToken(string strAPPID, string strSecret)
        {
            string str = "";
            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + strAPPID + "&secret=" + strSecret + "";
            System.Net.WebRequest wRequest = System.Net.WebRequest.Create(url);
            wRequest.Method = "GET";
            wRequest.ContentType = "text/html;charset=UTF-8";
            System.Net.WebResponse wResponse = wRequest.GetResponse();
            Stream stream = wResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
            str = reader.ReadToEnd();   //url返回的值  
            return str;
        }
 
        /// <summary>
        /// HTTP GET方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
 
            WebResponse response = null;
            string responseStr = null;
            try
            {
                response = request.GetResponse();
 
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }
            return responseStr;
        }
 
        /// <summary>
        /// 生成小程序二维码
        /// </summary>
        /// <param name="path">生成的小程序二维码存放的路径</param>
        /// <param name="scene">参数 最多32位</param>
        /// <param name="page">小程序页面路径 必须是已经发布了的</param>
        /// <param name="width">二维码大小</param>
        /// <param name="appid">小程序appid</param>
        /// <param name="secret">小程序密钥</param>
        /// <returns></returns>
        public static bool CreateXcxQrCode(string path,string scene,string page,string width,string appid,string secret)
        {
            try
            {
                string token = GetAccessToken(appid, secret);
                JObject tt =(JObject)JsonConvert.DeserializeObject(token);
                string _url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + tt["access_token"].ToString();
                string strURL = _url;
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                string options = "{\"scene\":\"" + scene + "\",\"page\":\"" + page + "\",\"width\":\"" + width + "\"}";
                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(options);
                request.ContentLength = payload.Length;
                System.IO.Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                System.Net.HttpWebResponse response;
                response = (System.Net.HttpWebResponse)request.GetResponse();
 
                System.IO.Stream s;
                s = response.GetResponseStream();
                byte[] val = StreamToBytes(s);
                System.IO.File.WriteAllBytes(path, val);
                s.Dispose();
                writer.Dispose();
            }
            catch(Exception e)
            {
                //log4net.LogManager.GetLogger("生成小程序二维码异常").Info(e);
                return false;
            }
            return true;
        }
        public static byte[] StreamToBytes(System.IO.Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();
        }