關於微信受權和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

相關文章
相關標籤/搜索