利用公众号JSSDK获取微信用户资料,如头像、昵称等

首先公众号(服务号)必须认证过,每年缴纳300元认证费。

然后在公众里面创建APPID和Secret,授权域名等操作,具体可以查看微信官方开发文档。

private string openId = "";
    private string accessToken = "";

    /// <summary>
    /// 获取微信用户资料,返回json
    /// </summary>
    /// <returns></returns>
    private string GetWxInfo()
    {
        string appId = "您的APPID";
        string secret = "您的Secret";
        
        //调用【网页授权获取用户信息】接口获取用户的openid和access_token
        GetOpenID(appId, secret);
        
        string url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";

        string result = HttpService.Get(url);
        
        return result;
    }

    /// <summary>
    /// 获取OPENID和access_token
    /// </summary>
    /// <param name="appId"></param>
    /// <param name="secret"></param>
    private void GetOpenID(string appId, string secret)
    {
        if (!string.IsNullOrEmpty(this.Request.QueryString["code"]))
        {
            //获取code码,以获取openid和access_token
            string code = this.Request.QueryString["code"];

            string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";

            string result = HttpService.Get(url);
            
            JsonData jd = JsonMapper.ToObject(result);
            openId = (string)jd["openid"];
            accessToken = (string)jd["access_token"];
        }
        else
        {
            string host = this.Request.Url.Host;
            string path = this.Request.Path;
            string redirect_uri = HttpUtility.UrlEncode("https://" + host + path);  //业务域名+路径,跟公众号后台设置必须一致

            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";

            try
            {
                //触发微信返回code码         
                this.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常
            }
            catch
            {

            }
        }
    }

正常情况下,微信会返回下述JSON数据包给公众号:

{
    "subscribe": 1, 
    "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", 
    "nickname": "Band", 
    "sex": 1, 
    "language": "zh_CN", 
    "city": "广州", 
    "province": "广东", 
    "country": "中国", 
    "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
    "subscribe_time": 1382694957,
    "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
    "remark": "",
    "groupid": 0,
    "tagid_list":[128,2],
    "subscribe_scene": "ADD_SCENE_QR_CODE",
    "qr_scene": 98765,
    "qr_scene_str": ""
}

微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId

小记:这里用的接口是网页授权token,另外一种是基础token,接口地址不同,开发的时候要注意别选错了。

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值