public string getContentsFromFile(string itemID, string mimetype) { return(new GoogleDriveAPI().getContentsFromFile(itemID, mimetype)); UserCredential credential; using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) { // The file token.json stores the user's access and refresh tokens, and is created // automatically when the authorization flow completes for the first time. string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); FilesResource.ExportRequest exportRequest = service.Files.Export(itemID, "text/plain"); var result = exportRequest.Execute(); return(result); }
public string getContentsFromFile(string itemID, string mimetype) { FilesResource.ExportRequest exportRequest = GetDriveServiceInstance().Files.Export(itemID, "text/plain"); var result = exportRequest.Execute(); return(result); }
private static void UploadFile(DriveService service) { File body = new File(); body.Title = "test image ocr - " + DateTime.Now.ToString(" - yyyyMMdd - HHmmss"); body.Description = "test image ocr - " + DateTime.Now.ToString(" - yyyyMMdd - HHmmss"); //body.MimeType = "application/vnd.ms-excel"; body.MimeType = "image/jpeg"; // File's content. byte[] byteArray = System.IO.File.ReadAllBytes("1.jpg"); MemoryStream stream = new MemoryStream(byteArray); try { //FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet"); FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.photo"); request.Ocr = true; request.OcrLanguage = "vi"; request.Convert = true; request.Upload(); File imgFile = request.ResponseBody; // Copy image and paste as document var textMetadata = new File(); //textMetadata.Name = inputFile.Name; //textMetadata.Parents = new List<string> { folderId }; textMetadata.MimeType = "application/vnd.google-apps.document"; FilesResource.CopyRequest requestCopy = service.Files.Copy(textMetadata, imgFile.Id); requestCopy.Fields = "id"; requestCopy.OcrLanguage = "vi"; var textFile = requestCopy.Execute(); // Now we export document as plain text FilesResource.ExportRequest requestExport = service.Files.Export(textFile.Id, "text/plain"); string output = requestExport.Execute(); // Uncomment the following line to print the File ID. // Console.WriteLine("File ID: " + file.Id); Console.WriteLine(output); } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } }
public OcrImageInfo goo_ocr_uploadFile(OcrImageInfo ocr) { string file = Path.Combine(PATH_OCR_IMAGE, ocr.FileName); if (File.Exists(file) == false) { ocr.StateOcr = STATE_OCR.OCR_FAIL_MISS_FILE; return(ocr); } DataFile body = new DataFile() { Title = ocr.FileName, Description = ocr.Url, //body.MimeType = "application/vnd.ms-excel"; MimeType = "image/jpeg" }; byte[] byteArray = File.ReadAllBytes(file); using (MemoryStream stream = new MemoryStream(byteArray)) { try { //FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet"); FilesResource.InsertMediaUpload request = gooService.Files.Insert(body, stream, "application/vnd.google-apps.photo"); request.Ocr = true; request.OcrLanguage = "vi"; request.Convert = true; request.Upload(); DataFile imgFile = request.ResponseBody; string fileId = imgFile.Id; // Copy image and paste as document var textMetadata = new DataFile(); //textMetadata.Name = inputFile.Name; //textMetadata.Parents = new List<string> { folderId }; textMetadata.MimeType = "application/vnd.google-apps.document"; FilesResource.CopyRequest requestCopy = gooService.Files.Copy(textMetadata, fileId); requestCopy.Fields = "id"; requestCopy.OcrLanguage = "vi"; var textFile = requestCopy.Execute(); // Now we export document as plain text FilesResource.ExportRequest requestExport = gooService.Files.Export(textFile.Id, "text/plain"); string output = requestExport.Execute(); ocr.TextResult = output; ocr.StateOcr = STATE_OCR.OCR_SUCCESS; writeLogMessage("OK: " + ocr.FileName); if (ocr.WriteToFile) { if (!string.IsNullOrEmpty(ocr.TextResult)) { File.WriteAllText(PATH_OCR_IMAGE + @"log\" + ocr.FileName + ".txt", ocr.TextResult); } } } catch (Exception e) { ocr.TextError = e.Message; ocr.StateOcr = STATE_OCR.OCR_FAIL_THROW_ERROR; } } return(ocr); }