/// <summary> /// 暴走账号登录 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <User> LoginAsync(string userName, string password) { //先清除Authorization信息 HttpBaseService.RemoveHeader("Authorization"); var result = await BaoZouOAuthAsync(userName, password); if (result != null && string.IsNullOrEmpty(result.Error)) { LoginPost loginPost = new LoginPost() { AccessToken = result.AccessToken, Source = "baozou", User = result.UserId }; User user = await GetUserInfo(loginPost); if (user != null) { HttpBaseService.SetHeader("Authorization", "Bearer " + user.AccessToken); } return(user); } return(null); }
private ApiService() { // 尝试从本地加载Authorization Header if (DataShareManager.Current.User != null && !string.IsNullOrEmpty(DataShareManager.Current.User.AccessToken)) { HttpBaseService.SetHeader("Authorization", "Bearer " + DataShareManager.Current.User.AccessToken); } }
protected async Task <ReturnT> PostDic <ReturnT>(Dictionary <string, string> dic, string uri) where ReturnT : class { try { string result = await HttpBaseService.PostDicAsync(dic, uri); return(JsonHelper.Deserlialize <ReturnT>(result)); } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
protected async Task <string> GetHtml(string url) { try { string html = await HttpBaseService.GetAsync(url); //byte[] bytes = Encoding.UTF8.GetBytes(html); //html = Encoding.GetEncoding("GBK").GetString(bytes); return(html); } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
/// <summary> /// 腾讯weibo登录 /// </summary> /// <returns></returns> public async Task <bool> TecentLoginAsync() { var result = await AuthenticationHelper.TencentAuthenticationAsync(); LoginPost loginPost = new LoginPost() { }; User user = await Post <LoginPost, User>(ServiceUri.Login, loginPost); if (user != null) { HttpBaseService.SetHeader("Authorization", "Bearer " + user.AccessToken); DataShareManager.Current.UpdateUser(user); return(true); } return(false); }
protected async Task <T> Post <T> (string uri, string body) where T : class { try { string json = await HttpBaseService.PostAsync(uri, body); if (json != null) { return(JsonHelper.Deserlialize <T>(json)); } return(null); } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
/// <summary> /// Post /// </summary> /// <typeparam name="T"></typeparam> /// <param name="uri"></param> /// <param name="t"></param> /// <returns></returns> protected async Task <JsonObject> Post <T> (string uri, T t) where T : class { string body = JsonHelper.Serializer(t); try { string json = await HttpBaseService.PostAsync(uri, body); if (json != null) { return(JsonObject.Parse(json)); } return(null); } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
protected async Task <ReturnT> Post <SendT, ReturnT>(string uri, SendT sendT) where ReturnT : class { string body = JsonHelper.Serializer(sendT); try { string json = await HttpBaseService.PostAsync(uri, body); if (json != null) { return(JsonHelper.Deserlialize <ReturnT>(json)); } return(null); } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
/// <summary> /// 新浪weibo登录 /// </summary> /// <returns></returns> public async Task <User> SinaWeiboLoginAsync() { //先清除Authorization信息 HttpBaseService.RemoveHeader("Authorization"); string result = await AuthenticationHelper.SinaAuthenticationAsync(); Regex regex = new Regex(@"(?<=code=)(.)*"); Match match = regex.Match(result); if (match.Success) { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("client_id", "3341101057"); dic.Add("client_secret", "0d0fe859e31afdc487d89fa3f3f0fe40"); dic.Add("grant_type", "authorization_code"); dic.Add("redirect_uri", "http://daily.ibaozou.com/sina_weibo/auth"); dic.Add("code", match.Value); var acccessToken = await PostDic <AccessTokenResult>(dic, ServiceUri.AccessToken); if (acccessToken != null) { LoginPost loginPost = new LoginPost() { AccessToken = acccessToken.AccessToken, Source = "sina", User = acccessToken.Uid, }; User user = await GetUserInfo(loginPost); if (user != null) { HttpBaseService.SetHeader("Authorization", "Bearer " + user.AccessToken); } return(user); } } return(null); }
/// <summary> /// 获取用户信息 /// </summary> /// <param name="login">login为null时将使用现有的验证信息刷新用户信息</param> /// <returns></returns> public async Task <User> GetUserInfo(LoginPost login = null) { if (login == null) { login = new LoginPost() { AccessToken = DataShareManager.Current.User.AccessToken, Source = "baozou", User = DataShareManager.Current.User.UserId }; } User user = await Post <LoginPost, User>(ServiceUri.Login, login); if (user != null) { HttpBaseService.SetHeader("Authorization", "Bearer " + user.AccessToken); } return(user); }
protected async Task <T> Delete <T>(string uri) where T : class { try { string json = await HttpBaseService.DeleteAsync(uri); if (json != null) { return(JsonHelper.Deserlialize <T>(json)); } else { return(null); } } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }
protected async Task <WriteableBitmap> GetImage(string url) { try { IBuffer buffer = await HttpBaseService.GetBytesAsync(url); if (buffer != null) { BitmapImage bi = new BitmapImage(); WriteableBitmap wb = null; using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { Stream stream2Write = stream.AsStreamForWrite(); await stream2Write.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length); await stream2Write.FlushAsync(); stream.Seek(0); await bi.SetSourceAsync(stream); wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight); stream.Seek(0); await wb.SetSourceAsync(stream); return(wb); } } else { return(null); } } catch (Exception e) { LogHelper.WriteLine(e); return(null); } }