小程序向微信公众号授权

小程序通过webview向公众号授权

1.设置公众号授权域名

2.小程序的webview的src通过带上小程序的openid以及其他的一些参数向网页授权域名下的网站发起请求。

ps:这里有个坑!!!!如果src的url太长,是的!太长!会导致参数发不过去!什么意思呢?好比,我的请求url是https://xxx.xxx/xxx/xxx?openid={{openid}}&xx={{xx}},到达url下的接口接收到的参数openid为空,那个xx也为空。我们之前是把openid,nickname,unionid都发过去,结果全为空,找了很久才发现这个问题。

3.微信公众号静默授权

 redirect_uri为授权回调地址

4.授权回调

 

复制代码
    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
复制代码

 

微信授权方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// <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");
}

  

 

posted @   zhaoshang  阅读(8938)  评论(0)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示