private async Task<bool> RefreshTokenIfNeed() { if (IsExpired(token)) { token = await KBApiUtil.RefreshTokenAsync(token.RefreshToken); FileUtil.writeTokenFile(token); } return true; }
// Check current time is exceed expiresTime in token or not. private bool IsExpired(KbToken token) { DateTime expiresTime = DateTime.Parse(token.ExpiresTime); DateTime nowTime = DateTime.Now; return nowTime > expiresTime; }
private static void SetHeaderAuthorization(WebClient webclient, KbToken token) { webclient.Headers["Authorization"] = KB_DEFAULT_TOKENTYPE + " " + token.AccessToken; }
// Make sure get token and it hasn't expired. private async Task<bool> CheckToken() { if (token == null) { token = FileUtil.readTokenFile(); } if (token == null) { (App.Current as App).RootFrame.Navigate(new Uri("/AuthPage.xaml", UriKind.Relative)); return false; } await RefreshTokenIfNeed(); return true; }
private static void UpdateExpiresTime(KbToken token) { token.ExpiresTime = DateTime.Now.AddSeconds(token.ExpiresIn).ToString(); }
public static void GetFileList(string path, KbToken token, DownloadStringCompletedEventHandler callback) { Uri pathUrl = new Uri(ListUrl + path); WebClient webclient = new WebClient(); SetHeaderAuthorization(webclient, token); webclient.DownloadStringCompleted += callback; webclient.DownloadStringAsync(pathUrl); }
private static Dictionary<string, string> GetAuthorizationHeader(KbToken token) { Dictionary<string, string> header = new Dictionary<string, string>(); header.Add("Authorization", KB_DEFAULT_TOKENTYPE + " " + token.AccessToken); return header; }
/// <summary> /// Download file from server. /// </summary> /// <param name="path"></param> /// <returns></returns> public static async Task<StorageFile> DownloadFileAsync(string path, KbToken token) { string url = DownloadUrl + Uri.EscapeDataString(path); Stream respStream = await doGetBytesAsync(url, GetAuthorizationHeader(token)); StorageFile sfile = await FileUtil.SaveStream(respStream, path); respStream.Close(); return sfile; }
/// <summary> /// Get account info from server. /// </summary> /// <param name="token"></param> /// <returns></returns> public static async Task<KbAccountInfo> GetAccountInfo(KbToken token) { string received = await doGetAsync(InfoUrl, GetAuthorizationHeader(token)); KbAccountInfo accountInfo = await JsonConvert.DeserializeObjectAsync<KbAccountInfo>(received); return accountInfo; }
/// <summary> /// Get file list in special path and check the hashcode to see if content was changed. /// </summary> /// <param name="path"></param> /// <param name="token"></param> /// <param name="hashcode"></param> /// <returns></returns> public static async Task<KbListInfo> GetFileListAsync(string path, KbToken token, string hashcode) { string url = ListUrl + path; if (hashcode != null) { url += "?hash=" + hashcode; } string received = await doGetAsync(url, GetAuthorizationHeader(token)); KbListInfo listInfo = await JsonConvert.DeserializeObjectAsync<KbListInfo>(received); return listInfo; }
/// <summary> /// Get file list in special path. /// </summary> /// <param name="path"></param> /// <param name="token"></param> /// <returns></returns> public static Task<KbListInfo> GetFileListAsync(string path, KbToken token) { return GetFileListAsync(path, token, null); }