Search code examples
google-apps-scriptgoogle-drive-apigoogle-slides-api

Error converting google slide to image using app script


This is the code I am using to convert my google slide to image:

function myFunction() {
  var dApp = DriveApp;
  var folder = dApp.getFoldersByName("til");
  
  var til = folder.next();
  var filesIter = til.getFiles();
  var i = 0;
  while(filesIter.hasNext() && i < 50){
    var file = filesIter.next();
    Logger.log(file.getId());
    
    exportSlideImages(file.getId(), file.getName())
    break;
    i = i + 1;
  }
  
}

function exportSlideImages(presentationId, imageName) {
  var presentation = SlidesApp.openById(presentationId);
  var baseUrl =
    "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
  var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    contentType: "application/json",
    muteHttpExceptions: true
  };
  
  presentation.getSlides().forEach(function(slide, i) {
    var url = baseUrl
      .replace("{presentationId}", presentationId)
      .replace("{pageObjectId}", slide.getObjectId());
    
    var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
    Logger.log(response)
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
    blob.setName(imageName +'.png');
    DriveApp.createFile(blob);
   
    
  });
}

It did create an image file but I got this output in the file:

"code": 403, "message": "Google Slides API has not been used in project 396798675804 before or it is disabled. Enable it by visiting (a link was given here to) then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",

But what I don't understand is I don't have any project with id 396798675804. What am I doing wrong?? Any pointers??


Solution

  • You have to enable the Google Slides Advanced service because your code is using UrlFetchApp to call the Slides API

    Related