-1

I am creating a presentation via Google.Apis.Slides.v1 in a C# project running on .NET 6.

I downloaded the latest nugget package:

Install-Package Google.Apis.Slides.v1

When trying to export the presentation as suggested by GPT and others.

This is my code:

private void downloadPresentation(SlidesService service, Presentation createdPresentation)
{
    // Export the presentation as a PPTX file
    var exportRequest = service.Presentations.Exports.Create(createdPresentation.PresentationId, new ExportRequest()
        {
            ExportFormat = "pptx",
        });

    var export = exportRequest.Execute();

    // Download the exported presentation
    var downloadRequest = service.Files.Get(export.Id);
    var stream = downloadRequest.ExecuteAsStreamAsync().Result;

    string downloadpath = string.Format(@"C:\temp\MyPresentation{0}.pptx", createdPresentation.PresentationId);

    using (var fileStream = new FileStream(downloadpath, FileMode.Create, FileAccess.Write))
    {
        stream.CopyTo(fileStream);
    }

    Console.WriteLine("File downloaded to: " + downloadpath);
}

I keep getting a compilation error:

'PresentationsResource' does not contain a definition for 'Exports' and no accessible extension method 'Exports' accepting a first argument of type

When I try to make a simple stream download and save it as pptx file it is not valid for any presentation program.

Does anyone have any idea why this happens?

Edit: it seems that as @ProgrammingLlama said there doesn't seem to be a reference for this in the docs. Does anyone know how I can download the file as a true .pptx?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
elitzur e
  • 65
  • 10
  • 4
    Code is missing. Also the [docs](https://developers.google.com/resources/api-libraries/documentation/slides/v1/csharp/latest/classGoogle_1_1Apis_1_1Slides_1_1v1_1_1PresentationsResource.html) don't indicate that `Exports` exists. Can you show the documentation that led you to believe it does? – ProgrammingLlama May 09 '23 at 09:22
  • sorry adding now – elitzur e May 09 '23 at 09:23

1 Answers1

1

As @ProgrammingLlama mentioned, the method Export doesn't exist in the Google Slide API. If you want to download a presentation, store in Google Drive. I will recommend using the Drive API instead. Which, it has an export option that allows you to get a Google Workspace document content in a format that your app can handle. For what you write in your question, it will be pptx.

I use the sample in the Google Documentation here, and made some small adjustments, like changing the MIME type based on this. This is just a sample, and you will need to make some changes as required.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Download;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

namespace DriveV3Snippets
{
    // Class sample for Drive export pptx
    public class Exportpptx
    {

        /// </summary>
        /// <param name="fileId">Id of the file.</param>
        /// <returns>Byte array stream if successful, null otherwise</returns>
        public static MemoryStream DriveExportpptx(string fileId)
        {
            try
            {
                /* Load pre-authorized user credentials from the environment.
                 TODO(developer) - See https://developers.google.com/identity for 
                 guides on implementing OAuth2 for your application. */
                GoogleCredential credential = GoogleCredential.GetApplicationDefault()
                    .CreateScoped(DriveService.Scope.Drive);

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Drive API Snippets"
                });

                var request = service.Files.Export(fileId, "application/vnd.openxmlformats-officedocument.presentationml.presentation");
                var stream = new MemoryStream();
                // Add a handler which will be notified on progress changes.
                // It will notify on each chunk download and when the
                // download is completed or failed.
                request.MediaDownloader.ProgressChanged +=
                    progress =>
                    {
                        switch (progress.Status)
                        {
                            case DownloadStatus.Downloading:
                            {
                                Console.WriteLine(progress.BytesDownloaded);
                                break;
                            }
                            case DownloadStatus.Completed:
                            {
                                Console.WriteLine("Download complete.");
                                break;
                            }
                            case DownloadStatus.Failed:
                            {
                                Console.WriteLine("Download failed.");
                                break;
                            }
                        }
                    };
                request.Download(stream);
                return stream;
            }
            catch (Exception e)
            {
                // TODO(developer) - handle error appropriately
                if (e is AggregateException)
                {
                    Console.WriteLine("Credential Not found");
                }
                else
                {
                    throw;
                }
            }
            return null;
        }
    }
}

Things to have in consideration:

This code sample uses the restricted drive scope that allows users to view and manage all of your Drive files. To learn more about Drive scopes, refer to API-specific authorization and authentication information.

Reference:

Giselle Valladares
  • 2,075
  • 1
  • 4
  • 13