/// <summary> /// Update both metadata and content of a file and return the updated file. /// </summary> public static Google.Apis.Drive.v2.Data.File UpdateResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String fileId, String newTitle, String newDescription, String newMimeType, String content, bool newRevision) { // First retrieve the file from the API. Google.Apis.Drive.v2.Data.File body = service.Files.Get(fileId).Fetch(); body.Title = newTitle; body.Description = newDescription; body.MimeType = newMimeType; //byte[] byteArray = Encoding.ASCII.GetBytes(content); byte[] byteArray = Encoding.UTF8.GetBytes(content); MemoryStream stream = new MemoryStream(byteArray); Google.Apis.Drive.v2.FilesResource.UpdateMediaUpload request = service.Files.Update(body, fileId, stream, newMimeType); request.Upload(); Permission newPermission = new Permission(); newPermission.Type = "anyone"; newPermission.Role = "reader"; newPermission.Value = ""; newPermission.WithLink = true; service.Permissions.Insert(newPermission, fileId).Fetch(); return(request.ResponseBody); }
/// <summary> /// Create a new file and return it. /// </summary> public static Google.Apis.Drive.v2.Data.File InsertResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String title, String description, String mimeType, String content) { // File's metadata. Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File(); body.Title = title; body.Description = description; body.MimeType = mimeType; byte[] byteArray = Encoding.UTF8.GetBytes(content); MemoryStream stream = new MemoryStream(byteArray); Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType); request.Upload(); var file = request.ResponseBody; Permission newPermission = new Permission(); newPermission.Type = "anyone"; newPermission.Role = "reader"; newPermission.Value = ""; newPermission.WithLink = true; service.Permissions.Insert(newPermission, file.Id).Fetch(); return(file); }
/// <summary> /// Create a new file and return it. /// </summary> public static Google.Apis.Drive.v2.Data.File InsertResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String title, String description, String mimeType, String content) { // File's metadata. Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File(); body.Title = title; body.Description = description; body.MimeType = mimeType; byte[] byteArray = Encoding.ASCII.GetBytes(content); MemoryStream stream = new MemoryStream(byteArray); Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType); request.Upload(); return(request.ResponseBody); }
public Google.Apis.Drive.v2.DriveService Init(string applicationName, string clientIdentifier, string clientSecret, string[] scope, bool logout, string refreshTokenFolder) { StatusForm.ShowDialog(); try { _refreshTokenFilePath = refreshTokenFolder; string fileDataStorePath = _refreshTokenFilePath; if (FileExists(fileDataStorePath)) { DateTime lastFileWriteTime = GetFileLastWriteTime(fileDataStorePath); if (lastFileWriteTime < new DateTime(2014, 8, 22)) { DeleteFile(fileDataStorePath); } } if (logout) { try { if (DirectoryExists(fileDataStorePath)) { string[] fileDataStoreFiles = System.IO.Directory.GetFiles(fileDataStorePath, "Google.Apis.Auth.OAuth2.Responses.*"); foreach (string fileDataStoreFile in fileDataStoreFiles) { DeleteFile(fileDataStoreFile); } } LoginForm.Logout(); } catch (Exception exception) { Log.Error(String.Format("Authenticator.Logout - Deleting token file. {0}",exception.Message)); } } var clientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets { ClientId = clientIdentifier, ClientSecret = clientSecret }; _fileDataStore = new EncryptedFileDataStore(refreshTokenFolder); System.Threading.Tasks.Task<Google.Apis.Auth.OAuth2.UserCredential> task = null; try { task = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync( clientSecrets, scope, System.Environment.UserName, System.Threading.CancellationToken.None, _fileDataStore); task.Wait(); } catch (Exception exception) { throw new LogException("GoogleWebAuthorizationBroker.AuthorizeAsync - " + exception.Message, false, false); } _userCredential = task.Result; var initializer = new BaseClientService.Initializer { HttpClientInitializer = _userCredential, ApplicationName = applicationName }; _driveService = new Google.Apis.Drive.v2.DriveService(initializer); UpdateAboutData(_driveService); return _driveService; } catch (Exception exception) { Log.Error(exception); return null; } finally { StatusForm.CloseDialog(); } }
public bool RefreshIfTimedOut(bool startBackgroundProcesses) { try { var stopWatch = new Log.Stopwatch(); if (IsSignedIn) { try { if (_refreshDateTime > DateTime.Now) { return(true); } Log.StartPerformance(ref stopWatch, "Begin - Google.Apis.Authentication.RefreshToken"); bool timedOut = Authenticator.HasTimedOut(); Log.EndPerformance(ref stopWatch, "End - Google.Apis.Authentication.RefreshToken"); if (!timedOut) { _refreshDateTime = DateTime.Now.AddSeconds(15); return(true); } } catch (Exception exception) { Log.Error(exception, false); } } IsSignedIn = false; _service = null; Log.StartPerformance(ref stopWatch, "Begin - Google.Apis.Authentication.Auth"); string tokenFilePath = GetRegistryEntryTokenFilePath(); if (String.IsNullOrEmpty(tokenFilePath)) { tokenFilePath = Settings.TokenNewFilePath; } _service = Authenticator.Init( Settings.Product, Settings.ClientID, Settings.ClientSecret, new[] { Google.Apis.Drive.v2.DriveService.Scope.Drive }, SignedOut, tokenFilePath); Log.EndPerformance(ref stopWatch, "End - Google.Apis.Authentication.Auth"); if (_service == null) { return(false); } _refreshDateTime = DateTime.Now.AddSeconds(15); IsSignedIn = true; SignedOut = false; UpdateRegistryEntries(tokenFilePath); if (startBackgroundProcesses || AutoStartBackgroundProcesses) { DriveService.StartBackgroundProcesses(); } return(true); } catch (Exception exception) { Log.Error(exception); return(false); } }
public Google.Apis.Drive.v2.DriveService Init(string applicationName, string clientIdentifier, string clientSecret, string[] scope, bool logout, string refreshTokenFolder) { StatusForm.ShowDialog(); try { _refreshTokenFilePath = refreshTokenFolder; string fileDataStorePath = _refreshTokenFilePath; if (FileExists(fileDataStorePath)) { DateTime lastFileWriteTime = GetFileLastWriteTime(fileDataStorePath); if (lastFileWriteTime < new DateTime(2014, 8, 22)) { DeleteFile(fileDataStorePath); } } if (logout) { try { if (DirectoryExists(fileDataStorePath)) { string[] fileDataStoreFiles = System.IO.Directory.GetFiles(fileDataStorePath, "Google.Apis.Auth.OAuth2.Responses.*"); foreach (string fileDataStoreFile in fileDataStoreFiles) { DeleteFile(fileDataStoreFile); } } LoginForm.Logout(); } catch (Exception exception) { Log.Error(String.Format("Authenticator.Logout - Deleting token file. {0}", exception.Message)); } } var clientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets { ClientId = clientIdentifier, ClientSecret = clientSecret }; _fileDataStore = new EncryptedFileDataStore(refreshTokenFolder); System.Threading.Tasks.Task <Google.Apis.Auth.OAuth2.UserCredential> task = null; try { task = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync( clientSecrets, scope, System.Environment.UserName, System.Threading.CancellationToken.None, _fileDataStore); task.Wait(); } catch (Exception exception) { throw new LogException("GoogleWebAuthorizationBroker.AuthorizeAsync - " + exception.Message, false, false); } _userCredential = task.Result; var initializer = new BaseClientService.Initializer { HttpClientInitializer = _userCredential, ApplicationName = applicationName }; _driveService = new Google.Apis.Drive.v2.DriveService(initializer); UpdateAboutData(_driveService); return(_driveService); } catch (Exception exception) { Log.Error(exception); return(null); } finally { StatusForm.CloseDialog(); } }
public bool RefreshIfTimedOut(bool startBackgroundProcesses) { try { var stopWatch = new Log.Stopwatch(); if (IsSignedIn) { try { if (_refreshDateTime > DateTime.Now) { return true; } Log.StartPerformance(ref stopWatch, "Begin - Google.Apis.Authentication.RefreshToken"); bool timedOut = Authenticator.HasTimedOut(); Log.EndPerformance(ref stopWatch, "End - Google.Apis.Authentication.RefreshToken"); if (!timedOut) { _refreshDateTime = DateTime.Now.AddSeconds(15); return true; } } catch (Exception exception) { Log.Error(exception, false); } } IsSignedIn = false; _service = null; Log.StartPerformance(ref stopWatch, "Begin - Google.Apis.Authentication.Auth"); string tokenFilePath = GetRegistryEntryTokenFilePath(); if (String.IsNullOrEmpty(tokenFilePath)) { tokenFilePath = Settings.TokenNewFilePath; } _service = Authenticator.Init( Settings.Product, Settings.ClientID, Settings.ClientSecret, new[] {Google.Apis.Drive.v2.DriveService.Scope.Drive}, SignedOut, tokenFilePath); Log.EndPerformance(ref stopWatch, "End - Google.Apis.Authentication.Auth"); if (_service == null) { return false; } _refreshDateTime = DateTime.Now.AddSeconds(15); IsSignedIn = true; SignedOut = false; UpdateRegistryEntries(tokenFilePath); if (startBackgroundProcesses || AutoStartBackgroundProcesses) { DriveService.StartBackgroundProcesses(); } return true; } catch (Exception exception) { Log.Error(exception); return false; } }