public void DownloadStyleList()
        {
            Task.Run(delegate
            {
                List <DriveFile> items = new List <DriveFile>();

                // Define parameters of request
                FilesResource.ListRequest listRequest = Service.Files.List();
                listRequest.Fields = "nextPageToken, files(id, name, trashed)";

                IList <Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;

                /*
                 * The listRequest.Execute() retrieves all files and folders from Drive, recursively
                 * Filter out irrelevant data, the requirement of a style file is that it ends with "-style.zip"
                 */

                foreach (var file in files)
                {
                    if (!((bool)file.Trashed) && file.Name.EndsWith(".zip", StringComparison.Ordinal))
                    {
                        items.Add(DriveFile.FromGoogleApiDriveFile(file));
                    }
                }

                if (ListDownloadComplete != null)
                {
                    ListDownloadComplete(null, new ListDownloadEventArgs {
                        DriveFiles = items
                    });
                }
            });
        }
Esempio n. 2
0
        public async Task <List <DriveFile> > DownloadFileList()
        {
            List <DriveFile> driveFiles = new List <DriveFile>();

            // TODO AccessToken lifespan is 60 minutes. Re-request when 60 minutes have passed
            if (AccessToken == null)
            {
                var response = await Authenticate();

                AccessToken = response.Token;
            }

            // Set mime time as application/zip so it would only return zip files
            string url = "https://www.googleapis.com/drive/v3/files?" + MimeType;

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("GData-Version", "3.0");
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);

                string result = await client.GetStringAsync(url);

                Variant variant = Variant.FromString(result);

                Variant files = variant.GetObjectElement("files");

                for (int i = 0; i < files.ArraySize; i++)
                {
                    Variant file = files.GetArrayElement(i);
                    driveFiles.Add(DriveFile.FromVariant(file));
                }
            }

            return(driveFiles);

            /*
             * {
             *       "kind": "drive#fileList",
             *       "incompleteSearch": false,
             *       "files": [
             *                {
             *                 "kind": "drive#file",
             *                 "id": "0By9YoP-GLAu8ZzlsM08yLUtIZkU",
             *                 "name": "bright-with-params-updated.zip",
             *                 "mimeType": "application/zip"
             *                },
             *                {
             *                 "kind": "drive#file",
             *                 "id": "0By9YoP-GLAu8WWdlVGZfemVZVzQ",
             *                 "name": "bright-cartocss-style-with-params.zip",
             *                 "mimeType": "application/zip"
             *                },
             *                {
             *                 "kind": "drive#file",
             *                 "id": "0By9YoP-GLAu8TktwcHk1X3VZZVk",
             *                 "name": "Magenta-Style.zip",
             *                 "mimeType": "application/zip"
             *                },
             *                {
             *                 "kind": "drive#file",
             *                 "id": "0By9YoP-GLAu8QkxlYTBSZDBEaUk",
             *                 "name": "blue-cartocss-style.zip",
             *                 "mimeType": "application/zip"
             *                },
             *                {
             *                 "kind": "drive#file",
             *                 "id": "0By9YoP-GLAu8LUhfQlVCQTNGMWc",
             *                 "name": "bright-cartocss-style.zip",
             *                 "mimeType": "application/zip"
             *                }
             *       ]
             *      }
             */
        }