C # public account development-webpage authorization (oauth2.0) to obtain basic user information 1,
Preface
Webpage authorization can be divided into two methods:Snsapi_base and snsapi_userinfo.Snsapi_base needs to follow the public account. The user authorization page is not displayed when obtaining user information. Snsapi_userinfo is used to browse the page without paying attention to the public account. A user authorization interface is displayed first, and user information can be obtained only after user authorization. First, let's look at the implementation of snsapi_base.
Attention
- Snsapi_base method when I started debugging. When I click the page address, the system prompts that the address link is unavailable. For webpage authorization, you must first set the basic information of webpage authorization in the public account, that is, set the domain name.
- Distinguish between global access_token and webpage authorization access_token
Enter the domain name only.
View results
Here, we can see that we have obtained basic user information through oauth snsapi_base, which is sufficient for general consulting development.
Thoughts
1. Get the code first by returning the address.
2. Get the openid through code.
3. Get the global access_token through appid and appsecret (this is different from snsapi_userinfo ).
4. obtain user information through global access_token and openid.
View code
This is easy to use. It mainly calls the get interface and prepares your get and post request methods. The return parameters are converted into object classes in json format.
Public ActionResult OAuthSnsApiBase () {string code = Request. QueryString ["code"]; try {if (! String. IsNullOrEmpty (code) {OAuthToken oauthToken = HttpUtility. Get <OAuthToken> (string. Format ("https://api.weixin.qq.com/sns/oauth2/access_token? Appid = {0} & secret = {1} & code = {2} & grant_type = authorization_code ", appID, appsecret, code); string accesstoken = string. empty; AccessToken token = HttpUtility. get <AccessToken> (string. format ("https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = {0} & secret = {1} ", appID, appsecret); if (token! = Null &&! String. IsNullOrEmpty (token. access_token) {accesstoken = token. access_token;} if (oauthToken! = Null &&! String. IsNullOrEmpty (oauthToken. openid) {OAuthUserInfo userInfo = HttpUtility. Get <OAuthUserInfo> (string. Format ("https://api.weixin.qq.com/cgi-bin/user/info? Access_token = {0} & openid = {1} & lang = zh_CN ", accesstoken, oauthToken. openid); if (userInfo! = Null) {ViewData ["headImage"] = userInfo. headimgurl; ViewData ["openid"] = userInfo. openid; ViewData ["nickName"] = userInfo. nickname; if (userInfo. sex = 0) {ViewData ["sex"] = "unknown";} else if (userInfo. sex = 1) {ViewData ["sex"] = "male";} else {ViewData ["sex"] = "female ";} viewData ["province"] = userInfo. province; ViewData ["city"] = userInfo. city;} else {}} else {return Redirect (strin G. Format ("https://open.weixin.qq.com/connect/oauth2/authorize? Appid = {0} & redirect_uri = {1} & response_type = code & scope = snsapi_base & state = 123456 # wechat_redirect ", appID," http: // "+ Request. url. host + Url. action ("OAuthSnsApiBase");} catch (Exception ex) {ViewData ["errmsg"] = ex. message;} return View ();}
public class OAuthToken { public string access_token { get; set; } public int expires_in { get; set; } public string refresh_token { get; set; } public string openid { get; set; } public string scope { get; set; } } public class AccessToken { public string access_token { get; set; } public int expires_in { get; set; } }public class OAuthUserInfo { public string openid { get; set; } public string nickname { get; set; } public int sex { get; set; } public string province { get; set; } public string city { get; set; } public string country { get; set; } public string headimgurl { get; set; } public string privilege { get; set; } public string unionid { get; set; } }
Summary
Here the interface for getting user information is https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ACCESS_TOKEN & openid = OPENID & lang = zh_CN. It is different from the user access interface obtained by webpage authorization 2.
Code all uploaded to github: https://github.com/garfieldzf8/WeChat
Reference
Http://www.cnblogs.com/txw1958/p/weixin76-user-info.html