zoukankan      html  css  js  c++  java
  • 关于MVC微信开发遇到的那些坑。

       首先让我吐槽下:微信就是一个渣渣,提供的开发文档都是坑。完全就是给他填坑  坑填完了 也就出来了。 先说下注意事项 然后提供源码吧。

     坑1:  Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://www.gsg.cq.cqtiandu.com/home/index&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"); 这个是第一步,估计很多人都已经死在这里了。 注意第一个必须是HTTPS 第二个 回调连接 是http 如果你填写https 也就在微信浏览器打不开了。

    坑2:这个是一个大坑大坑。 坑1的作用是拿出来code, 然后 var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, appsecret, codes);来拿出openid 和access_token。 这个地方 code 可以拿出来,我TM secret不对都拿出来了,整了一天。就是返回40029(code 无效) 我用你给的code居然说code无效。然后找啊找啊 找不出来原因。肯定访问一次 而且肯定没超过5分钟。然后第二天就把密钥重置了下。然后可以了。这个SB 微信给了个错误的appsecret。(《!--  不是同事拉着我我就砍死MHT了 !--》)

    坑3: 微信里面是不支持跳转的,在第一步拿取code的时候是有个 回调连接的,这个必须写到你的要展现页面,如果写到一个方法 方法在跳转页面。是跳不过去的。

    坑4:其他小坑都是自己问题,只能说明微信不人性化给的接口太复杂。多次恶心。 下面给源码。

      public void Index1(string id)
            {
                   
                    //弹出授权页面(如在不弹出授权页面基础下未获得openid,弹出授权页面,提示用户授权)
                    if (id == "1")
                    {
                        Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://www.gsg.cq.cqtiandu.com/home/index&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
                    }
                    else
                    {
                        //不弹出授权页面
                        Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://www.gsg.cq.cqtiandu.com/home/index&response_type=code&scope=snsapi_base&state=1#wechat_redirect");     
                    
                    }
                
            }// 这个测试的时候用 /地址/home/index1/0 生成二维码然后微信扫描。刚进入官网也是用这个 生成的连接 
      public ActionResult Index()
            {
                string codes = Request["code"];
                string codes1 = Request.Form["code"];
                string codes2 = Request.RawUrl;
                var client = new System.Net.WebClient();
                client.Encoding = System.Text.Encoding.UTF8;
                var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, appsecret, codes);
                HttpWebResponse hp = GetResponse(url);
                string htmlval = Html(hp);
                string logins = htmlval.Replace("{", "").Replace("}", "").Replace('"', ' ');
                string[] log = logins.Split(',');
                if (log.Length > 3)
                {
                    string[] OK = log[3].Split(':');
                    string openid = OK[1].ToString();
                    string[] access = log[0].Split(':');
                    string access_token = access[1].ToString();
                    LoginUser lguser = new LoginUser
                    {
                        UserId = openid.Trim(),
                        access_token = access_token
                    };
                    Session["Lguser"] = lguser;
                    SqlParameter[] pms = { 
                                         new SqlParameter("@openid",openid),
                                         new SqlParameter("@access_token",access_token)
                                         };
                    MessasgeInfor mginfor = Datafun.Mgfunctioninfor("insert into tb_openid(openid,access_token) values(@openid,@access_token)", pms);
                }
    
                
                MessasgeData mgdata = Datafun.MgfunctionData("select * from tb_tel");
                ViewBag.tel = mgdata.Mgdata.Rows[0]["tel"];       
                return View();
            }这个是首页 也就是拿取openid,我只要openid 所以就那个Openid 就是分割处理了




    /*
    20151216

    scope=snsapi_base 这个是获取不到用户信息的,会报48001 API未授权错误
    scope=snsapi_userinfo才可以


    */

      

  • 相关阅读:
    使用公用表表达式的递归查询
    cocos2d-x 精灵的创建和基本使用
    全栈project师的毁与誉
    使用zTree和json构建简单树节点
    使用贝赛尔曲线画扇形、圆形、弧线、多边形,实现App下载时的动画效果demo
    UIBezierPathStudyDemo
    iOS-swift环形进度指示器+图片加载动画
    IOS之以UIBezierPath绘制饼状图
    Swift之UIBezierPath
    swift app中展示折线图, 饼状图, 柱状图等数据图表
  • 原文地址:https://www.cnblogs.com/kainjie/p/4935763.html
Copyright © 2011-2022 走看看