public ActionResult GetGoogleDriveAuthAsync(string devicePlanId) { PlatformGoogle pg = new PlatformGoogle(_env.ContentRootPath); pg.AuthRequestUrl(Request.Host.Value); return(Redirect(pg.AuthRequestUrl(Request.Host.Value) + "&state=" + WebUtility.UrlEncode(devicePlanId))); }
public List <FileListModel> GetGoogleDriveList(int planId, string directory, PlatformInsertModel planData) { PlatformGoogle gg = new PlatformGoogle(_env.ContentRootPath); dynamic platformDetail = JObject.Parse(planData.JsonData); string token = ""; if (WebUtilities.ConvertDatabaseDateTime(platformDetail.tokenExpire.ToString()) > DateTime.Now) { token = platformDetail.accessToken; } else { var trp = gg.AuthCodeToAccessToken("", "", platformDetail.refreshToken.ToString()); dynamic dData = null; if (!String.IsNullOrEmpty(planData.JsonData)) { dData = JObject.Parse(planData.JsonData); } List <KeyValuePair <string, string> > list = new List <KeyValuePair <string, string> >(); list.Add(new KeyValuePair <string, string>("accountId", planId.ToString())); list.Add(new KeyValuePair <string, string>("accessToken", trp.AccessToken)); if (!String.IsNullOrEmpty(trp.RefreshToken)) { list.Add(new KeyValuePair <string, string>("refreshToken", trp.RefreshToken)); } else if (dData != null) { list.Add(new KeyValuePair <string, string>("refreshToken", dData.refreshToken.ToString())); } list.Add(new KeyValuePair <string, string>("tokenExpire", DateTime.Now.AddSeconds(trp.ExpiresInSeconds).ToString())); string jsonData = ""; foreach (var item in list) { string header = item.Key; string data = item.Value; jsonData += "\"" + header + "\": \"" + data + "\","; } jsonData = jsonData.Substring(0, jsonData.Length - 1); jsonData = "{ " + jsonData + " }"; planData.JsonData = jsonData; operations.Plan.UpdatePlan(planData, GetOrganizationId()); token = trp.AccessToken; } GoogleDriveAuthInformation inf = new GoogleDriveAuthInformation() { access_token = token }; return(gg.GetDirectoryList(directory, inf.access_token)); }
private TokenResponse ResetGoogleDriveToken(PlatformInsertModel planData, int planId, DpOperations db, int organizationId) { PlatformGoogle pg = new PlatformGoogle(_env.ContentRootPath); dynamic dData = null; if (!String.IsNullOrEmpty(planData.JsonData)) { dData = JObject.Parse(planData.JsonData); } TokenResponse trp = pg.AuthCodeToAccessToken("", "", dData.refreshToken.ToString()); List <KeyValuePair <string, string> > list = new List <KeyValuePair <string, string> >(); list.Add(new KeyValuePair <string, string>("accountId", planId.ToString())); list.Add(new KeyValuePair <string, string>("accessToken", trp.AccessToken)); if (!String.IsNullOrEmpty(trp.RefreshToken)) { list.Add(new KeyValuePair <string, string>("refreshToken", trp.RefreshToken)); } else if (dData != null) { list.Add(new KeyValuePair <string, string>("refreshToken", dData.refreshToken.ToString())); } list.Add(new KeyValuePair <string, string>("tokenExpire", DateTime.Now.AddSeconds(trp.ExpiresInSeconds.Value).ToString())); string jsonData = ""; foreach (var item in list) { string header = item.Key; string data = item.Value; jsonData += "\"" + header + "\": \"" + data + "\","; } jsonData = jsonData.Substring(0, jsonData.Length - 1); jsonData = "{ " + jsonData + " }"; planData.JsonData = jsonData; db.Plan.UpdatePlan(planData, organizationId); return(trp); }
public TokenResponse exchangeCode(string code, string redirectUri) { PlatformGoogle pg = new PlatformGoogle(_env.ContentRootPath); return(pg.AuthCodeToAccessToken(code, redirectUri, "")); }
public void SendFile(string model) { BackupLog bc = new BackupLog(); string localDirectory = ""; string remoteDirectory = ""; string devicePlanId = ""; dynamic platformDetail = JObject.Parse(model); localDirectory = platformDetail.localDirectory; remoteDirectory = platformDetail.remoteDirectory; devicePlanId = platformDetail.devicePlanId; bc.DevicePlanId = Convert.ToInt32(devicePlanId); bc.CreateDirectoryCount = 0; bc.CreateFileCount = 0; bc.FailedCount = 0; bc.ProccessCount = 0; bc.TotalSize = 0; bc.DeletedCount = 0; bc.UpdatedCount = 0; DbOperations db = new DbOperations(); IPlatform platf = null; if (platformDetail.planType.ToString() == "1") { DriveService s = new Google.Apis.Drive.v3.DriveService(); var services = new DriveService(new BaseClientService.Initializer() { ApiKey = platformDetail.googleApiCode, // from https://console.developers.google.com (Public API access) ApplicationName = "ECloud Backup", }); platf = new PlatformGoogle(s, platformDetail.googleAccessToken.ToString(), platformDetail.googleApiCode.ToString(), Common.ConvertDatabaseDateTime(platformDetail.googleTokenExpired.ToString())); } else if (platformDetail.planType.ToString() == "2") { platf = new AmazonS3(platformDetail.apiAccessKey.ToString(), platformDetail.apiSecretKey.ToString(), platformDetail.region.ToString()); } DateTime dtStartBackup = DateTime.Now; SendBackupDirectory(localDirectory, remoteDirectory, db, Convert.ToInt32(devicePlanId), platf, bc); var list = db.GetErrorFileList(Convert.ToInt32(devicePlanId), dtStartBackup); List <string> errorList = new List <string>(); foreach (var k in list) { errorList.Add(k.FullName + " - " + k.ErrorMessage); } LogDeviceModel mdl = new LogDeviceModel { apiAccessKey = bc.apiAccessKey, apiSecretKey = bc.apiSecretKey, cpuId = bc.cpuId, deviceId = bc.deviceId, devicePlanId = Convert.ToInt32(devicePlanId), diskVolume = bc.diskVolume, errorList = errorList, macAddress = bc.macAddress }; IRestRequest request = new RestRequest("api/device/deviceplanid/{deviceplanid}/finish", Method.POST); request.RequestFormat = RestSharp.DataFormat.Json; request.AddParameter("deviceplanid", devicePlanId, ParameterType.UrlSegment); request.AddBody(bc); TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>(); RestRequestAsyncHandle handle = client.ExecuteAsync(request, r => { taskCompletion.SetResult(r); }); IRestResponse response = taskCompletion.Task.Result; IRestRequest request1 = new RestRequest("api/device/log", Method.POST); request1.RequestFormat = RestSharp.DataFormat.Json; request1.AddBody(mdl); TaskCompletionSource <IRestResponse> taskCompletion1 = new TaskCompletionSource <IRestResponse>(); RestRequestAsyncHandle handle1 = client.ExecuteAsync(request1, r => { taskCompletion1.SetResult(r); }); IRestResponse response1 = taskCompletion1.Task.Result; }