/// <summary> /// 普通数据请求 /// </summary> /// <param name="type">请求数据类型</param> /// <param name="data">参数包(SdkCmdBase的子类)</param> /// <returns>请求返回数据包括返回数据和错误信息</returns> public SdkResponse RequestCmd(SdkRequestType type, ISdkCmdBase data) { RestRequest request = new RestRequest(); mSdkResponse = new SdkResponse(); bool retValue = ConfigParams(request, type, data).Result; if (false != retValue) { if (null != data && !string.IsNullOrEmpty(SdkData.AccessToken)) { request.AddHeader("Authorization", string.Format("OAuth2 {0}", SdkData.AccessToken)); } else { //如需鉴权传入OAuth2.0的accessToken request.AddParameter("source", SdkData.AppKey); } IRestResponse response = m_Client.Execute(request); HandleEx(response as RestResponse); } else if (mSdkResponse.errCode == SdkErrCode.XPARAM_ERR && mSdkResponse.content == Constants.SdkMsg.TokenNull) { // 重新授权. // ClientOAuth oauth = new ClientOAuth(); // oauth.BeginOAuth(); } return(mSdkResponse); }
/// <summary> /// 普通数据请求 /// </summary> /// <param name="type">请求数据类型</param> /// <param name="data">参数包(SdkCmdBase的子类)</param> /// <returns>请求返回数据包括返回数据和错误信息</returns> public IAsyncOperation <SdkResponse> RequestCmd(SdkRequestType type, ISdkCmdBase data) { return(System.Runtime.InteropServices.WindowsRuntime.AsyncInfo.Run <SdkResponse>((token) => Task.Run <SdkResponse>(async() => { RestRequest request = new RestRequest(); mSdkResponse = new SdkResponse(); bool retValue = ConfigParams(request, type, data).Result; if (false != retValue) { if (null != data && !string.IsNullOrEmpty(SdkData.AccessToken)) { request.AddHeader("Authorization", string.Format("OAuth2 {0}", SdkData.AccessToken)); } else { //如需鉴权传入OAuth2.0的accessToken request.AddParameter("source", SdkData.AppKey); } IRestResponse response = await m_Client.ExecuteAsync(request); HandleEx(response as RestResponse); } else if (mSdkResponse.errCode == SdkErrCode.XPARAM_ERR && mSdkResponse.content == Constants.SdkMsg.TokenNull) { // 重新授权. ClientOAuth oauth = new ClientOAuth(); oauth.BeginOAuth(); } return mSdkResponse; }, token))); }
/// <summary> /// 组参. /// </summary> /// <param name="request"></param> /// <param name="type"></param> /// <param name="data"></param> /// <returns></returns> private async Task <bool> ConfigParams(RestRequest request, SdkRequestType type, ISdkCmdBase data) { Action <string> errAction = (e1) => { mSdkResponse.content = e1; mSdkResponse.errCode = SdkErrCode.XPARAM_ERR; }; if (null == request) { errAction(Constants.SdkMsg.SDKInnerErr); return(false); } m_Client.BaseUrl = Constants.PublicApiUrl; if (SdkData.AccessToken != null) { request.AddParameter("access_token", SdkData.AccessToken); } else { errAction(Constants.SdkMsg.TokenNull); return(false); } switch (type) { case SdkRequestType.POST_MESSAGE: { CmdPostMessage message = null; if (data is CmdPostMessage) { message = data as CmdPostMessage; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Method = Method.POST; request.Resource = Constants.WeiboAPI.PostMsg; if (message.Status.Length > 0) { request.AddParameter("status", message.Status); } if (message.ReplyId.Length > 0) { request.AddParameter("in_reply_to_status_id", message.ReplyId); } if (message.Lat.Length > 0) { request.AddParameter("lat", message.Lat); } if (message.Long.Length > 0) { request.AddParameter("long", message.Long); } if (message.Annotations.Length > 0) { request.AddParameter("annotations", message.Annotations); } } break; case SdkRequestType.POST_MESSAGE_PIC: { CmdPostMsgWithPic message = null; if (data is CmdPostMsgWithPic) { message = data as CmdPostMsgWithPic; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Method = Method.POST; request.Resource = Constants.WeiboAPI.PostMsgPic; request.AddParameter("status", message.Status); if (message.Lat.Length > 0) { request.AddParameter("lat", message.Lat); } if (message.Long.Length > 0) { request.AddParameter("long", message.Long); } if (0 == message.PicPath.Length) { errAction(Constants.SdkMsg.PicPathEmpty); return(false); } string picType = System.IO.Path.GetExtension(message.PicPath); string picName = System.IO.Path.GetFileName(message.PicPath); // await request.AddFileAsync("pic", message.PicPath); } break; case SdkRequestType.FRIENDS_TIMELINE: { CmdTimelines message = null; if (data is CmdTimelines) { message = data as CmdTimelines; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.FriendTimeline; request.Method = Method.GET; if (message.SinceID.Length > 0) { request.AddParameter("since_id", message.SinceID); } if (message.MaxID.Length > 0) { request.AddParameter("max_id", message.MaxID); } if (message.Count.Length > 0) { request.AddParameter("count", message.Count); } if (message.Page.Length > 0) { request.AddParameter("page", message.Page); } if (message.BaseApp.Length > 0) { request.AddParameter("base_app", message.BaseApp); } if (message.Feature.Length > 0) { request.AddParameter("feature", message.Feature); } } break; case SdkRequestType.USER_TIMELINE: { CmdUserTimeline message = null; if (data is CmdUserTimeline) { message = data as CmdUserTimeline; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.UserTimeline; request.Method = Method.GET; if (message.UserId.Length > 0) { request.AddParameter("user_id", message.UserId); } if (message.ScreenName.Length > 0) { request.AddParameter("screen_name", message.ScreenName); } if (message.SinceID.Length > 0) { request.AddParameter("since_id", message.SinceID); } if (message.MaxID.Length > 0) { request.AddParameter("max_id", message.MaxID); } if (message.Count.Length > 0) { request.AddParameter("count", message.Count); } if (message.Page.Length > 0) { request.AddParameter("page", message.Page); } if (message.BaseApp.Length > 0) { request.AddParameter("base_app", message.BaseApp); } if (message.Feature.Length > 0) { request.AddParameter("feature", message.Feature); } } break; case SdkRequestType.FRIENDSHIP_CREATE: { CmdFriendShip message = null; if (data is CmdFriendShip) { message = data as CmdFriendShip; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.CreateFriendShip; request.Method = Method.POST; if (message.UserId.Length > 0) { request.AddParameter("user_id", message.UserId); } if (message.ScreenName.Length > 0) { request.AddParameter("screen_name", message.ScreenName); } } break; case SdkRequestType.FRIENDSHIP_DESDROY: { CmdFriendShip message = null; if (data is CmdFriendShip) { message = data as CmdFriendShip; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.DestroyFriendShip; request.Method = Method.POST; if (message.UserId.Length > 0) { request.AddParameter("user_id", message.UserId); } if (message.ScreenName.Length > 0) { request.AddParameter("screen_name", message.ScreenName); } } break; case SdkRequestType.FRIENDSHIP_SHOW: { CmdFriendShip message = null; if (data is CmdFriendShip) { message = data as CmdFriendShip; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.ShowFriendShip; request.Method = Method.GET; if (message.SourceId.Length > 0) { request.AddParameter("source_id", message.SourceId); } if (message.SourceScreenName.Length > 0) { request.AddParameter("source_screen_name", message.SourceScreenName); } if (message.UserId.Length > 0) { request.AddParameter("target_id", message.UserId); } if (message.ScreenName.Length > 0) { request.AddParameter("target_screen_name", message.ScreenName); } } break; case SdkRequestType.AT_USERS: { CmdAtUsers atUsers = null; if (data is CmdAtUsers) { atUsers = data as CmdAtUsers; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.AtUser; request.Method = Method.GET; if (atUsers.Keyword.Length > 0) { request.AddParameter("q", atUsers.Keyword); } if (atUsers.Count.Length > 0) { request.AddParameter("count", atUsers.Count); } if (atUsers.Range.Length > 0) { request.AddParameter("range", atUsers.Range); } if (atUsers.Type.Length > 0) { request.AddParameter("type", atUsers.Type); } } break; case SdkRequestType.SHORTEN_URL: { CmdShortenUrl shortUrl = null; if (data is CmdShortenUrl) { shortUrl = data as CmdShortenUrl; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } request.Resource = Constants.WeiboAPI.ShortenUrl; request.Method = Method.GET; if (shortUrl.OriginalUrl.Length > 0) { request.AddParameter("url_long", shortUrl.OriginalUrl); } } break; case SdkRequestType.OTHER_API: { ICustomCmdBase customData = null; if (data is ICustomCmdBase) { customData = data as ICustomCmdBase; } else { errAction(Constants.SdkMsg.ParamTypeErr); return(false); } customData.ConvertToRequestParam(request); } break; default: { errAction(Constants.SdkMsg.RequestNotSupppt); return(false); } } request.AddParameter("curtime", DateTime.Now.Ticks.ToString()); return(true); }
void weiboRequestCompleted(SdkRequestType requestType, SdkResponse response) { Deployment.Current.Dispatcher.BeginInvoke(() => { txtInput.IsReadOnly = false; if (response.errCode == SdkErrCode.SUCCESS) { MessageBox.Show("发送成功"); } else { MessageBox.Show("发送失败"); } }); }
private void LoadSinaWeiboContentComplete(SdkRequestType requestType, SdkResponse response) { try { if (response.errCode == SdkErrCode.SUCCESS) { WStatuses statuses = null; DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WStatuses)); statuses = ser.ReadObject(response.stream) as WStatuses; Deployment.Current.Dispatcher.BeginInvoke(() => { if (statuses.statuses != null) { foreach (WStatus status in statuses.statuses) { ItemViewModel model = SinaWeiboModelConverter.ConvertItemToCommon(status); if (model != null) { App.ViewModel.SinaWeiboItems.Add(model); } } } m_progressIndicatorHelper.PopTask(); } ); } // 失败 else { Deployment.Current.Dispatcher.BeginInvoke(() => { // 21327 expired_token Token 过期 if (response.specificCode == "21327") { MessageBox.Show("新浪微博帐号已过期,请重新登陆", ">_<", MessageBoxButton.OK); // 清掉保存的当前帐号信息 // 但是关注人信息还保留着 PreferenceHelper.RemoveSinaWeiboLoginAccountPreference(); } m_progressIndicatorHelper.PopTask(); }); } } catch (System.Exception ex) { UmengSDK.UmengAnalytics.reportError(ex); Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show("新浪微博加载过程中发生未知错误", "悲剧了>_<", MessageBoxButton.OK); m_progressIndicatorHelper.PopTask(); }); } }