1.1.微信公众号开发----获取个人用户信息(2)---获取微信用户信息

根据开发者文档获取用户信息分为4步: 获取code(授权标识)--->通过code掉微信接口获取OpenId(微信公众号每个用户的唯一标识)-->获取accessToken(调接口标识)---》通过OpenId、accessToken 掉接口获取 用户信息。

前面已经提到获取code,https://blog.csdn.net/wangwengrui40/article/details/103710839

那么我们怎么利用code获取 用户信息呢

一.获取openid

获取openid,直接调用微信的接口

Appid 与  secret开发者密码 在微信公众号内

1.1.微信公众号开发----获取个人用户信息(2)---获取微信用户信息_第1张图片

        public async Task GetUserMessageCode(string code)
        {
           HttpClient httpClient = new HttpClient();//C#封装的Http访问接口的方法,其他语言切换
            var getParameter = await httpClient.GetStringAsync("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" 
+ 你自己的Appid+ "&secret=" +你自己的开发者密码 + "&code=" + code + "&grant_type=authorization_code");
            //得到的参数序列化
            var usermesage = JsonConvert.DeserializeObject(getParameter);
            return usermesage.openid;
        }
    public class GetUserCodeDto
    {/// 
     /// access_token
     /// 
        public string access_token { get; set; }
        /// 
        /// openid
        /// 
        public string openid { get; set; }
    }

二.获取AccessToken

        /// 
        /// 得到ACCESS_TOKEN
        /// 
        /// 
        public async Task GetACCESS_TOKEN()
        {
            HttpClient httpClient = new HttpClient();//C#封装的Http访问接口的方法,其他语言切换
            var getParameter = await httpClient.GetStringAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
 +你自己的appid + "&secret=" + 你自己的secret);
            var access_token = JsonConvert.DeserializeObject(getParameter);
            return access_token.access_token;
        }
    public class AccessTokenDto
    {/// 
     /// access_token
     /// 
        public string access_token { get; set; }
    }

三.获取用户信息

        public async Task GetUserMessage(string openid, string lang = "zh_CN")
        {
           HttpClient httpClient = new HttpClient();//C#封装的Http访问接口的方法,其他语言切换
            var getParameter = await httpClient.GetStringAsync("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + 获取到的access_token + "&openid=" +  获取到的openid + "&lang=" + lang);
            var magess2 = JsonConvert.DeserializeObject(getParameter);
            return magess2;
        }
    public class GetUserDto
    {
        public int? subscribe { get; set; }
        public string openid { get; set; }
        public string nickname { get; set; }
        public int? sex { get; set; }
        public string language { get; set; }
        public string city { get; set; }
        public string province { get; set; }
        public string country { get; set; }
        public string headimgurl { get; set; }
        public int? subscribe_time { get; set; }
        public int? unionid { get; set; }
        public string remark { get; set; }
        public int groupid { get; set; }
        public string[] tagid_list { get; set; }
        public string subscribe_scene { get; set; }
        public int? qr_scene { get; set; }
        public string qr_scene_str { get; set; }
    }

 

你可能感兴趣的:(微信公众号开发)