public byte[] Download(GDriveCredentials cred, string fileId) { var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = cred.Credential, ApplicationName = "GMT", }); var l = service.Files.List(); var list = l.Execute(); File file1 = null; foreach (var f in list.Files) { if (fileId.Equals(f.Name, System.StringComparison.OrdinalIgnoreCase)) { file1 = f; } } if (file1 == null) { return(null); } //https://developers.google.com/drive/v3/web/manage-downloads var request = service.Files.Get(file1.Id); using (var s = new MemoryStream()) { request.Download(s); return(s.ToArray()); } }
/// <summary> /// Authorize this instance. /// to setup credentials : https://console.developers.google.com/apis/library /// https://console.developers.google.com/home/dashboard?project=gantt-mono-tracker /// A problem with W10 /// https://github.com/google/google-api-dotnet-client/issues/557 /// Use access_type offline with the Google C# SDK to prevent the need to reauthenticate every hour /// https://gist.github.com/SNiels/d2d39276bdeaeaa4d6b6148a0ab02a48 /// </summary> public async Task AuthorizeAsync() { //TODO: exceptions from async methods hangs app. var credFile = "Sunchronization\\GDrive\\client_id.json"; string credPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string resultPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); credPath = Path.Combine(credPath, credFile); using (var stream = new FileStream(credPath, FileMode.Open, FileAccess.Read)) { //Scopes for use with the Google Drive API var secrets = GoogleClientSecrets.Load(stream).Secrets; Credentials = new GDriveCredentials(secrets.ClientId, secrets.ClientSecret); Credentials.Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( secrets, new[] { Uri.EscapeUriString(DriveService.Scope.Drive) }, Environment.UserName, CancellationToken.None); }; }
public bool Upload(GDriveCredentials cred, byte[] raw, string fileId, bool exists) { var service = new DriveService(new BaseClientService.Initializer { HttpClientInitializer = cred.Credential, ApplicationName = "GMT", }); var body = new File { Name = fileId, Description = "Gantt Mono Tracker project", MimeType = "text/xml" }; //manual //https://developers.google.com/drive/v3/web/manage-uploads FilesResource.CreateMediaUpload request; using (var stream = new MemoryStream(raw)) { if (exists) { try { FilesResource.UpdateMediaUpload updateRequest = service.Files.Update(body, fileId, stream, "text/xml"); var updateProcess = updateRequest.Upload(); if (updateProcess.Status != Google.Apis.Upload.UploadStatus.Completed) { throw updateProcess.Exception; } } catch { exists = false; } } if (!exists) { request = service.Files.Create( body, stream, "text/xml"); request.Fields = "id"; /*{ * "error": { * "errors": [ * { * "domain": "global", * "reason": "insufficientPermissions", * "message": "Insufficient Permission" * } * ], * "code": 403, * "message": "Insufficient Permission" * * } * }*/ var process = request.Upload(); if (process.Status != Google.Apis.Upload.UploadStatus.Completed) { throw process.Exception; } } } return(true); }