public JsonResult GetFolders() { SampleModel sm = new SampleModel(); sm.access_token = ""; string fileName = Server.MapPath(Url.Content("~/Content/token.file")); if (System.IO.File.Exists(fileName)) { StreamReader sr = new StreamReader(fileName); string tem = sr.ReadToEnd(); sr.Close(); CustomJsonClass _json = JsonConvert.DeserializeObject <CustomJsonClass>(tem); sm.access_token = _json.access_token; DateTime JsonExpiryTime = DateTime.Parse(_json.time); double SecondsDifference = DateTime.Now.Subtract(JsonExpiryTime).TotalSeconds; if (SecondsDifference > 3000) { var client = new RestClient("https://login.live.com/oauth20_token.srf"); var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddParameter("undefined", String.Format("client_id={1}&grant_type=refresh_token&redirect_uri={2}&client_secret={3}&refresh_token={0}&undefined=", _json.refresh_token, client_id, HttpUtility.UrlEncode(redirect_url), HttpUtility.UrlEncode(client_secret)), ParameterType.RequestBody); IRestResponse response = client.Execute(request); string tt = response.Content; StreamWriter sw = new StreamWriter(fileName); sw.WriteLine(response.Content.Replace("}", String.Format(",\"time\":\"{0} {1}\"", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString())) + "}"); sw.Flush(); sw.Close(); } List <FolderListClass> _folders = new List <FolderListClass>(); List <FolderTempClass> _foldersObject = new List <FolderTempClass>(); _folders.Add(new FolderListClass { id = "root", text = "root" }); foreach (var Res in ResFromFolder(_foldersObject, "root", sm.access_token).ToList()) { getHierarchy(Res, _folders[0].children, _foldersObject, sm.access_token); } return(this.Json(_folders, JsonRequestBehavior.AllowGet)); } return(this.Json("", JsonRequestBehavior.AllowGet)); }
public ActionResult CreateToken() { SampleModel sm = new SampleModel(); sm.access_token = ""; string fileName = Server.MapPath(Url.Content("~/Content/token.file")); if (System.IO.File.Exists(fileName)) { StreamReader sr = new StreamReader(fileName); string tem = sr.ReadToEnd(); sr.Close(); CustomJsonClass _json = JsonConvert.DeserializeObject <CustomJsonClass>(tem); sm.access_token = _json.access_token; DateTime JsonExpiryTime = DateTime.Parse(_json.time); double SecondsDifference = DateTime.Now.Subtract(JsonExpiryTime).TotalSeconds; if (SecondsDifference > 3000) { return(Redirect(String.Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&scope={1}&response_type=code&redirect_uri={2}", client_id, scope, redirect_url))); } } else { return(Redirect(String.Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&scope={1}&response_type=code&redirect_uri={2}", client_id, scope, redirect_url))); } ViewBag.MyString = "Token already Created"; return(View()); }