Micro letter - page authorized user to obtain basic information

Original link: http://www.cnblogs.com/deeround/p/4371906.html

Micro-letter web authorization, access to basic user information

. 1          #region acquires user information
 2          public WeixinUser GetUserInfo ( String code)
 . 3          {
 . 4              // . 1, the page is authorized to snsapi_base scope initiated, is used to get the user to enter a page of openid, and is authorized to automatically skip silent callback page. It is perceived by the user directly into the callback page (often a business page)
 5              // 2, page authority to snsapi_userinfo as the scope initiated, is used to obtain the basic information of the user. However, this requires the user to manually authorize consent, and because the user agrees, so I do not need attention, you can get the basic information of the user after authorization.
6              // 3 for the number of users has been public concern, if the user is authorized to enter the page number from the pages of this public session public numbers or custom menu, even scope for the snsapi_userinfo, authorization is silent, no user perception. 
. 7  
. 8              String URL = "" , requestStr = "" ;
 . 9              // the first step to code
 10              // URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1234567890000 & the redirect_uri the redirect_uri & response_type = = = snsapi_base code & scope State = & # wechat_redirect. 1 ";
 . 11              // callback address code and will bring State
 12 is              // second step OpenID obtain the access_token 
13 is              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 the access_token = the GetParameter ( " the access_token " , requestStr); // page authorized the access_token 
16              String OpenID the GetParameter = ( " OpenID " , requestStr);
 . 17  
18 is  
. 19              // third step acquires the user information 
20              // / // a manner access_token using web authorization does not return the Subscribe 
21              // snsapi_userinfo attention has authorization page does not appear there will not concern authorization page
 22              // snsapi_base has focused on gaining the basic information that is not concerned about unauthorized prompt api (Note: once concerned about or have authorization before, you may also be able to obtain basic information)
 23              // url = " https://api.weixin.qq.com/sns/userinfo?access_token="+ + Access_token" OpenID & = "OpenID + +" & lang = zh_CN ";
 24              // requestStr = browser.DownloadString (new new Uri (URL));
 25  
26 is              // Second way // plain access_token acquired user can ignore the scope basic information (Note: does not concern the user returns only subscribe and openid) 
27              the access_token = the AccessToken (); // common the access_token 
28              url = " https://api.weixin.qq.com/cgi-bin/user/info?access_token = " + the access_token + " & OpenID = " + OpenID + " & lang = zh_CN " ;
 29  
30              requestStr = Browser.DownloadString(new Uri(url));
31             return ToWeixinUser(requestStr);
32         }
33 
34         #endregion

 

 

The following different cases are received by the server data

First, the use of basic access_token get basic user information

  1, has been concerned about the micro-channel users

{"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, is not concerned about the micro-channel users

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

 

 

Second, use the web authorization access_token

  1, sope = when snsapi_base:

    ① does not follow the user (again micro-channel or an authorized user not following)

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

    ② not concerned about the user (who had now canceled focus attention or have had authorized)

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

    Users concerned about ③

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

    ① not concerned about the user

      Authorization page will pop up a prompt to get nicknames, titles and other information, click OK to log

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

    ② has been concerned about the user

      We will not jump out of an authorization page, direct return to the user data

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

 

 

Above, just a personal test case, does not mean all the scenes, if wrong, reminded me, thank you! !

 

Reproduced in: https: //www.cnblogs.com/deeround/p/4371906.html

Guess you like

Origin blog.csdn.net/weixin_30345577/article/details/94785455