关于微信受权和unionid 的获取思路。

1.首先根据appid 获取到预受权码的codehtml

  string Appid = "******";//appid。因为网页受权与js-jdk使用不一样微信,因此暂时独立于此处。
                    string redirect_uri = "http://*********";//这里是回调地址的url
                    string state = filterContext.HttpContext.Request.Url.ToString();//这里的state是
                    state = state.Replace("?", "|").Replace("&", "!!");
                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";
                    filterContext.HttpContext.Response.Redirect(url);

2.根据预受权码获取我的的用户信息json

//根据code获取预受权码asstoken 和 openid 和Unionid
     string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + df.getAppid() + "&secret=" + df.getAppSecret() + "&code=" + code + "&grant_type=authorization_code"; WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = wc.DownloadData(url);   string htmlstr = System.Text.Encoding.GetEncoding("utf-8").GetString(pageData);
    
//根据预受权码获取用户我的信息
df.get_json方法是获取当前json字符串的当前节点的值
    url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + df.get_json(htmlstr, "access_token") + "&openid=" + df.get_json(htmlstr, "openid") + "&lang=zh_CN";
    wc = new WebClient();
    wc.Credentials = CredentialCache.DefaultCredentials;
    pageData = wc.DownloadData(url);
    htmlstr = System.Text.Encoding.GetEncoding("utf-8").GetString(pageData);
    return htmlstr;

3.若是想要根据openid 获取Unionid 则须要如下步奏api

 3.1根据appid 和appscreat 获取当前公众号的asstoken 微信

 

 3.2根据openid和UnionId获取用户信息(这里的用户基本信息只能是关注的才能获取彻底基本信息,未关注只能获取openid和unionid)app

相关文章
相关标签/搜索