protected void Page_Load(object sender, EventArgs e) { xrwang.net.Message.Insert(new xrwang.net.Message(xrwang.net.MessageType.Request, Request.RawUrl)); string code = RequestEx.TryGetQueryString("code"); string state = RequestEx.TryGetQueryString("state"); if (!string.IsNullOrWhiteSpace(code)) { ltrResult.Text += "OAuth code:" + code + ",State:" + state; string userName = "******"; ErrorMessage errorMessage; OAuthAccessToken token = OAuthAccessToken.Get(userName, code, out errorMessage); if (errorMessage.IsSuccess) { ltrResult.Text += "<br/>获取网页授权成功。" + token.ToString(); token = OAuthAccessToken.Refresh(userName, token.refresh_token, out errorMessage); if (errorMessage.IsSuccess) { ltrResult.Text += "<br/>刷新网页授权成功。" + token.ToString(); errorMessage = OAuthAccessToken.CheckValidate(token.access_token, token.openid); if (errorMessage.IsSuccess) { ltrResult.Text += "<br/>校验网页授权成功。"; UserInfo user = OAuthAccessToken.GetUserInfo(token.access_token, token.openid, out errorMessage); ltrResult.Text += "<br/>获取用户信息:" + (errorMessage.IsSuccess ? user.nickname + user.sex.Value.ToString("g") + user.headimgurl : errorMessage.ToString()); } else { ltrResult.Text += "<br/>校验网页授权失败。" + errorMessage.ToString(); } } else { ltrResult.Text += "<br/>刷新网页授权失败。" + errorMessage.ToString(); } } else { ltrResult.Text += "获取网页授权失败。" + errorMessage.ToString(); } } }
/// <summary> /// 获取OAuth用户基本信息 /// </summary> /// <param name="accessToken">网页access token</param> /// <param name="openId">用户id</param> /// <param name="errorMessage">返回获取是否成功</param> /// <returns>返回用户基本信息;如果获取失败,返回null。</returns> public static UserInfo GetOAuthUserInfo(string accessToken, string openId, out ErrorMessage errorMessage) { return(OAuthAccessToken.GetUserInfo(accessToken, openId, out errorMessage)); }