C#微信大眾號開發--網頁受權(oauth2.0)獲取用戶根本信息一。本站提示廣大學習愛好者:(C#微信大眾號開發--網頁受權(oauth2.0)獲取用戶根本信息一)文章只能為提供參考,不一定能成為您想要的結果。以下是C#微信大眾號開發--網頁受權(oauth2.0)獲取用戶根本信息一正文
微信網頁受權共分為兩種方式:snsapi_base、snsapi_userinfo。 snsapi_base需求關注大眾號,獲取用戶信息時不彈出用戶受權界面。 snsapi_userinfo是在用戶未關注大眾號的狀況下閱讀頁面,會先彈出一個用戶受權界面,用戶受權後才干拿到用戶信息。這一篇我們先看snsapi_base完成。
踩坑留意


這裡留意只填寫到域名
看效果
這裡看我們經過oauth snsapi_base曾經拿到用戶的根本信息,關於普通征詢類的開發也就夠用了。

看思緒
1、經過回掉地址先拿到code。
2、經過code拿到openid。
3、經過appid和appsecret拿到全局access_token(這一點區別於snsapi_userinfo)。
4、經過全局access_token和openid拿到用戶信息。
看代碼
到這裡也就復雜了,次要是get方式調微信接口,同時預備好你的get和post懇求方式,前往參數用json轉化成實體類。
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"] = "未知";
}
else if (userInfo.sex == 1)
{
ViewData["sex"] = "男";
}
else
{
ViewData["sex"] = "女";
}
ViewData["province"] = userInfo.province;
ViewData["city"] = userInfo.city;
}
else
{
}
}
else
{
}
}
else
{
return Redirect(string.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; }
}
總結
這裡獲取用戶信息的接口是https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN。和網頁受權二獲取用戶接口是不一樣的。


代碼全部上傳到github裡:https://github.com/garfieldzf8/WeChat
參考http://www.cnblogs.com/txw1958/p/weixin76-user-info.html