/// <summary> /// 设置用户token信息 /// </summary> /// <param name="UserId">用户标识</param> /// <param name="AccessToken">token不存在</param> private void SetAccessToken(string UserId, string AccessToken, LoggingSessionInfo loggingSessionInfo, string resultErrorUrl) { try { if (UserId == null || UserId.Trim().Equals("")) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("新浪回调页面2-用户标识为空") }); } Response.Write("设置用户token信息1"); #region 处理业务 #region 判断用户是否存在会员表中 VipBLL vipServer = new VipBLL(loggingSessionInfo); VipEntity vipInfo = vipServer.GetByID(UserId); if (vipInfo == null || vipInfo.VIPID == null || vipInfo.VIPID.Equals("")) { vipInfo.VIPID = UserId; vipInfo.VipCode = vipServer.GetVipCode(); vipInfo.ClientID = loggingSessionInfo.CurrentUser.customer_id; vipInfo.Status = 1; vipServer.Create(vipInfo); } #endregion #region 判断用户是否存在会员的新浪微博扩展表中 VipExpandSinaWbBLL vipSinaWbServer = new VipExpandSinaWbBLL(loggingSessionInfo); VipExpandSinaWbEntity vipSinaWbInfo = new VipExpandSinaWbEntity(); vipSinaWbInfo = vipSinaWbServer.GetByID(UserId); if (vipSinaWbInfo != null && vipSinaWbInfo.VipId != null && !vipSinaWbInfo.VipId.Equals("")) { vipSinaWbInfo.AccessToken = AccessToken; vipSinaWbServer.Update(vipSinaWbInfo, false); } else { VipExpandSinaWbEntity vipSinaWbInfo1 = new VipExpandSinaWbEntity(); vipSinaWbInfo1.VipId = UserId; vipSinaWbInfo1.AccessToken = AccessToken; vipSinaWbServer.Create(vipSinaWbInfo1); } #endregion #endregion Loggers.Debug(new DebugLogInfo() { Message = string.Format("新浪回调页面3-设置用户信息成功.") }); } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("新浪回调页面4-错误信息提示 {0}:" + ex.ToString()) }); Response.Write(ex.ToString()); } }
public void GetUserInfo(string UserId, string AccessToken, LoggingSessionInfo loggingSessionInfo, string uid) { try { //string uid = "1036433880"; Sina = new Client(new OAuth(ConfigurationManager.AppSettings["AppKey"], ConfigurationManager.AppSettings["AppSecret"], AccessToken, null)); //用cookie里的accesstoken来实例化OAuth,这样OAuth就有操作权限了 var user = Sina.API.Dynamic.Users.Show(uid); string strUser = user.ToString(); var userInfo = strUser.DeserializeJSONTo <SinaWeiBoUserInfo>(); #region 更新用户信息 VipExpandSinaWbBLL vipSinaWbServer = new VipExpandSinaWbBLL(loggingSessionInfo); VipExpandSinaWbEntity vipSinaWbInfo = new VipExpandSinaWbEntity(); vipSinaWbInfo.VipId = UserId; if (userInfo != null && userInfo.id != null && !userInfo.id.Equals("")) { vipSinaWbInfo.ScreenName = userInfo.screen_name; vipSinaWbInfo.LabelName = userInfo.name; vipSinaWbInfo.Province = userInfo.province; vipSinaWbInfo.City = userInfo.city; vipSinaWbInfo.Location = userInfo.location; vipSinaWbInfo.Description = userInfo.description; vipSinaWbInfo.Url = userInfo.url; vipSinaWbInfo.ProfileImageUrl = userInfo.profile_image_url; vipSinaWbInfo.ProfileUrl = userInfo.profile_url; vipSinaWbInfo.Gender = userInfo.gender; vipSinaWbInfo.Weihao = userInfo.weihao; vipSinaWbServer.Update(vipSinaWbInfo, false); } #endregion //Response.Write(string.Format("{0}", user)); Response.Write("设置新浪微博用户信息成功."); } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("设置新浪微博用户信息失败:" + ex.ToString()) }); Response.Write(ex.ToString()); } }
/// <summary> /// 授权查询 /// </summary> private void GetAccessToken(string UserId, string AccessToken, LoggingSessionInfo loggingSessionInfo, string resultErrorUrl) { try { #region 获取授权信息 var uriString = "https://api.weibo.com/oauth2/get_token_info"; var postData = "access_token=" + AccessToken + ""; Loggers.Debug(new DebugLogInfo() { Message = string.Format("支付跳转-链接字符串: {0}", postData) }); WebClient myWebClient = new WebClient(); // 注意这种拼字符串的ContentType myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // 转化成二进制数组 byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 上传数据,并获取返回的二进制数据. byte[] responseArray = myWebClient.UploadData(uriString, "POST", byteArray); var data = System.Text.Encoding.UTF8.GetString(responseArray); var tokenInfo = data.DeserializeJSONTo <TokenInfo>(); if (tokenInfo == null) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("授权查询3-授权成功--但是没有返回信息:" + data.ToString()) }); return; } #endregion #region 更新授权信息 VipExpandSinaWbBLL vipSinaWbServer = new VipExpandSinaWbBLL(loggingSessionInfo); VipExpandSinaWbEntity vipSinaWbInfo = new VipExpandSinaWbEntity(); vipSinaWbInfo = vipSinaWbServer.GetByID(UserId); if (vipSinaWbInfo != null && vipSinaWbInfo.VipId != null && !vipSinaWbInfo.VipId.Equals("")) { vipSinaWbInfo.AccessToken = AccessToken; if (tokenInfo != null) { vipSinaWbInfo.UID = ToStr(tokenInfo.uid); vipSinaWbInfo.Appkey = ToStr(tokenInfo.appkey); vipSinaWbInfo.Scope = ToStr(tokenInfo.scope); vipSinaWbInfo.CreateAt = ToStr(tokenInfo.create_at); vipSinaWbInfo.ExpireIn = ToStr(tokenInfo.expire_in); } vipSinaWbServer.Update(vipSinaWbInfo, false); } else { VipExpandSinaWbEntity vipSinaWbInfo1 = new VipExpandSinaWbEntity(); vipSinaWbInfo1.VipId = UserId; if (tokenInfo != null) { vipSinaWbInfo1.UID = ToStr(tokenInfo.uid); vipSinaWbInfo1.Appkey = ToStr(tokenInfo.appkey); vipSinaWbInfo1.Scope = ToStr(tokenInfo.scope); vipSinaWbInfo1.CreateAt = ToStr(tokenInfo.create_at); vipSinaWbInfo1.ExpireIn = ToStr(tokenInfo.expire_in); } vipSinaWbServer.Create(vipSinaWbInfo1); } #endregion Loggers.Debug(new DebugLogInfo() { Message = string.Format("授权查询3-授权成功:") }); //设置新浪微博用户信息 GetUserInfo(UserId, AccessToken, loggingSessionInfo, ToStr(tokenInfo.uid)); } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("授权查询4-错误信息提示 {0}:" + ex.ToString()) }); Response.Write(ex.ToString()); } }