微信--网页授权获取用户基本信息

微信网页授权,获取用户基本信息

 1         #region 获取用户信息
 2         public WeixinUser GetUserInfo(string code)
 3         {
 4             //1、以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)
 5             //2、以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。
 6             //3、对于已关注公众号的用户,如果用户从公众号的会话或者自定义菜单进入本公众号的网页授权页,即使是scope为snsapi_userinfo,也是静默授权,用户无感知。
 7 
 8             string url = "", requestStr = "";
 9             //第一步 获取code
10             //url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1234567890000&redirect_uri=redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
11             //回调地址会带上code和state
12             //第二步 获取access_token openid
13             url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
14             requestStr = browser.DownloadString(new Uri(url));
15             string access_token = GetParameter("access_token", requestStr);//网页授权access_token
16             string openid = GetParameter("openid", requestStr);
17 
18 
19             //第三步 获取用户信息
20             ////方式一 //使用网页授权access_token 不会返回subscribe
21             //snsapi_userinfo 已关注不会出现授权页面 未关注会出现授权页面
22             //snsapi_base 已关注获取到基本信息 未关注提示api 未授权 (注:曾经关注过或者曾经授权过的,或许也能获取到基本信息)
23             //url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
24             //requestStr = browser.DownloadString(new Uri(url));
25 
26             //方式二 //使用普通access_token 可以无视scope获取到用户基本信息(注:未关注用户只返回subscribe和openid)
27             access_token = AccessToken();//普通access_token
28             url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
29 
30             requestStr = browser.DownloadString(new Uri(url));
31             return ToWeixinUser(requestStr);
32         }
33 
34         #endregion

 

 

以下是不同情况服务器接收到的数据

一,使用基础access_token获取用户基本信息

  1,已关注微信用户

{"subscribe":1,"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY","nickname":"玩什么","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/w6o7zE9nXBUnhMcud68Z007X2HZl9XVdUTYexCsmTzeHxNuDor4Ar1KaYwVcURubL5bxKZGRicXCyjibtLu1fEeKM6VvmveeUib\/0","subscribe_time":1427351478,"remark":""}

  2,未关注微信用户

{"subscribe":0,"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY"}

 

 

二,使用网页授权access_token

  1,sope=snsapi_base时:

    ① 未关注用户(重来没有关注或授权的微信用户)

{"errcode":48001,"errmsg":"api unauthorized"}

    ② 未关注用户(曾经关注过现在取消关注或曾经授权过的)

{"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY","nickname":"玩什么","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/w6o7zE9nXBUnhMcud68Z007X2HZl9XVdUTYexCsmTzeHxNuDor4Ar1KaYwVcURubL5bxKZGRicXCyjibtLu1fEeKM6VvmveeUib\/0","privilege":[]}

    ③ 关注的用户

{"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY","nickname":"玩什么","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/w6o7zE9nXBUnhMcud68Z007X2HZl9XVdUTYexCsmTzeHxNuDor4Ar1KaYwVcURubL5bxKZGRicXCyjibtLu1fEeKM6VvmveeUib\/0","privilege":[]}

 

  2,scope=snsapi_userinfo

    ① 未关注用户

      会跳出一个授权页面,提示要获取昵称、头衔等信息,点击确认登录

{"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY","nickname":"玩什么","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/w6o7zE9nXBUnhMcud68Z007X2HZl9XVdUTYexCsmTzeHxNuDor4Ar1KaYwVcURubL5bxKZGRicXCyjibtLu1fEeKM6VvmveeUib\/0","privilege":[]}

    ② 已关注用户

      不会跳出一个授权页面,直接返回用户数据

{"openid":"oMpnlsydc2FAwfaBP2WmBwG-c_HY","nickname":"玩什么","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/w6o7zE9nXBUnhMcud68Z007X2HZl9XVdUTYexCsmTzeHxNuDor4Ar1KaYwVcURubL5bxKZGRicXCyjibtLu1fEeKM6VvmveeUib\/0","privilege":[]}

 

 

以上情况,只是个人测试情况,不代表所有场景,如有错误,提醒下我,谢谢!!

 

微信--网页授权获取用户基本信息

上一篇:微信--自定义菜单(创建和删除)


下一篇:android第三方(qq、微信、微博)登录