static void Main(string[] args)
{
// first: create report (export file) with List & Label
Console.WriteLine("List & Label Report is creating...");
string fileToUpload = CreateLLExportFile();
if (!string.IsNullOrEmpty(fileToUpload) && System.IO.File.Exists(fileToUpload))
{
// second: upload the exported file into Google Drive
Console.WriteLine("List & Label Report was created successfully; now it becomes uploaded...");
if (GoogleDriveUpload(fileToUpload))
{
Console.WriteLine("info: file could be uploaded successfully.");
}
else
{
Console.WriteLine("error: file could not be uploaded successfully!");
}
} Console.ReadLine();
}
Im ersten Schritt wird ein gewöhnlicher Export mit List & Label mit Hilfe der ExportConfiguration-Klasse erstellt:
private static string CreateLLExportFile()
{
string exportedFile = string.Empty;
using(ListLabel LL = new ListLabel())
{
try
{
// define licensing info for List & Label - see also PersonalLicense.txt
LL.LicensingInfo = "";
// first: set the List & Label datasource
string xmlDataSourceFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data.xml");
XmlDataProvider provider = new XmlDataProvider(xmlDataSourceFile);
LL.DataSource = provider;
// second: configure and execute the export
exportedFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LL_GoogleDrive.pdf");
ExportConfiguration expConfig = new combit.ListLabel20.ExportConfiguration(LlExportTarget.Pdf, exportedFile, "test.lst");
LL.Export(expConfig);
}
catch (ListLabelException ex)
{
Console.WriteLine("Information: " + ex.Message + "\n\nThis information was generated by a List & Label custom exception.");
}
}
return exportedFile;
}
(Anbei finden Sie eine ZIP-Datei welche die im Beispiel verwendete Projektdatei (Druckvorlage) und XML-Datenquelle enthält. Diese müssen sich im gleichen Verzeichnis wie die erstellte Anwendung befinden.)
Die von List & Label erzeugte Report-Datei kann nun mit Hilfe der Google Drive API hochgeladen werden. Hierfür verlangt die API allerdings diverse Credentials. Um die benötigten Informationen CLIENT_ID und CLIENT_SECRET zu erhalten, aktivieren Sie zunächst die Google Drive API und erstellen Sie einen API Access (Create an OAuth 2.0 client ID). Weitere Informationen und eine Anleitung hierzu finden Sie unter unten stehendem Link von Google. Bitte beachten Sie auch die Hinweise im Google-Link, dass der hier verwendete Authentifizierungsprozess stark vereinfacht ist. Tragen Sie dann diese Daten wie folgt in den Code ein:
private static readonly string CLIENT_ID = "YOUR CLIENT_ID"; private static readonly string CLIENT_SECRET = "YOUR CLIENT_SECRET";
Sobald die Credentials dann zur Verfügung stehen, kann der eigentliche Upload mit Hilfe des Google Drive SDK realisiert werden:
private static bool GoogleDriveUpload(string fileToUpload)
{
bool succeeded = false;
try
{
// first: create/generate credentials UserCredential
credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = CLIENT_ID, ClientSecret = CLIENT_SECRET, }, new[] { DriveService.Scope.Drive }, "user", CancellationToken.None).Result;
// second: create the service with the specified credentials
var service = new DriveService(new BaseClientService.Initializer()
{HttpClientInitializer = credential, ApplicationName = "Google Drive API Sample", });
// third: upload the file with the specified service File
body = new File();
body.Title = "LL Test document";
body.Description = "A List & Label document";
body.MimeType = "application/pdf";
byte[] byteArray = System.IO.File.ReadAllBytes(fileToUpload);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, body.MimeType);
request.Upload();
File file = request.ResponseBody;
if ( file != null && !string.IsNullOrEmpty(file.Id) )
{
succeeded = true;
}
}
catch (Google.GoogleApiException ex)
{
Console.WriteLine("Information: " + ex.Message);
} return succeeded;
}
Hinweis:
Für die einmalige Authentifizierung, so dass ohne weitere Benutzer-Aktion auf den Google Drive Account hochgeladen werden kann, verwendet die Google Drive API eine Schlüsseldatei, die auf dem System unter "%APPDATA%\Google.Apis.Auth" gespeichert wird.
Links:
https://www.combit-support.net/de/support/files/cmbtkb/KBTD000860_Data.ziphttps://developers.google.com/workspace/drive/api/quickstart/js?hl=de
Verwandte Artikel:
https://forum.combit.net/t/list-label-mit-dropbox/4912