小程序向微信公衆號受權

小程序經過webview向公衆號受權web

1.設置公衆號受權域名json

2.小程序的webview的src經過帶上小程序的openid以及其餘的一些參數向網頁受權域名下的網站發起請求。小程序

ps:這裏有個坑!!!!若是src的url太長,是的!太長!會致使參數發不過去!什麼意思呢?比如,個人請求url是https://xxx.xxx/xxx/xxx?openid={{openid}}&xx={{xx}},到達url下的接口接收到的參數openid爲空,那個xx也爲空。咱們以前是把openid,nickname,unionid都發過去,結果全爲空,找了好久才發現這個問題。api

3.微信公衆號靜默受權微信

 redirect_uri爲受權回調地址app

4.受權回調ide

 

    public class PublicNumberCodeController : Controller
    {
        // GET: PublicNumberCode
        public ActionResult Index()
        {
            LogHelper.Info("***已經到達PublicNumberCode頁***:" + DateTime.Now);
            try
            {
                //若是已經受權,直接進入頁面
                if (!Util.isNotNull(Request["code"]))
                {
                    LogHelper.Info("已經受權過了");
                    return View();
                }
                int weixin_id = Convert.ToInt32(Request["weixin_id"]); //公衆號id
                string fromopenid = Request["openid"]; //openid
                string appcode = Request["appcode"]; //appcode
                string code = Request["code"];
                string state = Request["state"];
                string appid = Request["appid"];
                string appsecret = Request["appsecret"];
                
                LogHelper.Info("code值:" + code);
                string token_jsonstr = WeixinAPi.accesstoken(code, appid, appsecret);
                //根據 code 換取 token ,獲得  openid 與 access_token
                if (token_jsonstr.IndexOf("openid") > -1 && token_jsonstr.IndexOf("access_token") > -1)
                {
                    JObject token_json = JObject.Parse(token_jsonstr);
                    //公衆號的openid
                    string openid = token_json["openid"].ToString();
                    string access_token = token_json["access_token"].ToString();
                    //插入數據

                    ***********關聯小程序openid與公衆號openid**************
                    
                    //查詢是否有未發成功的模板消息,一個小時之內的
                    LogHelper.Info("***準備發送模板消息***:" + DateTime.Now);

                   *************發送待支付模板消息**************************
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
            }
            LogHelper.Info("***完成,到引導頁***:" + DateTime.Now);
            return View();
        }
View Code

 

微信受權方法網站

        /// <summary>
        /// 第一步:用戶贊成受權,獲取code
        /// </summary>    
        /// <param name="redirect_uri">受權後重定向的回調連接地址,請使用urlencode對連接進行處理</param>  
        /// <param name="scope">應用受權做用域,snsapi_base (不彈出受權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出受權頁面</param>    
        /// <param name="state">重定向後會帶上state參數,開發者能夠填寫a-zA-Z0-9的參數值,最多128字節 </param>  
        /// <returns>null</returns>        
        public static void authorize(string AppId, string redirect_uri, string scope, string state)
        {

            HttpResponse res = HttpContext.Current.Response;
            string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AppId + "&redirect_uri=" + HttpUtility.UrlEncode(redirect_uri) + "&response_type=code&scope=" + scope + "&state=" + state + "#wechat_redirect";
            //sys.errMessage = url;
            res.Redirect(url);
        }


        /// <summary>
        /// 第二步:經過code換取網頁受權access_token
        /// </summary>    
        /// <param name="code">填寫第一步獲取的code參數 </param>  
        /// <returns>json string</returns>        
        public static  string accesstoken(string code, string appid, string appsecret)
        {
            string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
            return Util.MethodGET(url, "UTF-8");
        }

        /// <summary>
        /// 第三步:經過access_token換取unionid
        /// </summary>    
        /// <param name="access_token">填寫第一步獲取的code參數 </param>
        /// <param name="openid">openid</param>
        /// <returns>json string</returns>        
        public static  string getunionid(string access_token, string openid)
        {
            string url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN ";
            return Util.MethodGET(url, "UTF-8");
        }
相關文章
相關標籤/搜索