Search code examples
google-apps-scripturlfetchgoogle-slides-api

"Invalid JSON payload received" with Google Slides API in Apps Script


I've seen a couple of threads on this topic but none helped. I try to change the URL of an image in a Google Slides, in Apps Script, but not using the Presentation helper (because I cannot).

function updateSlides() {
  let presentationId = "XXXXXXXX"
  let requests = [{
    ReplaceImageRequest: {
      imageObjectId: "g13fc473bd83_2_0",
      imageReplaceMethod: "CENTER_INSIDE",
      url: "https://pbs.twimg.com/card_img/1553040603776225280/MOtK57RF?format=jpg"
    }
  }]
  let params = {
    method: 'POST',
    muteHttpExceptions: true,
    contentType: 'application/json',
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
    },
    payload: JSON.stringify({
      requests: requests
    })
  }

  let response = UrlFetchApp.fetch('https://slides.googleapis.com/v1/presentations/' + presentationId + ':batchUpdate', params);
  console.log(response.getContentText())
}

And I get this error

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"ReplaceImageRequest\" at 'requests[0]': Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "requests[0]",
            "description": "Invalid JSON payload received. Unknown name \"ReplaceImageRequest\" at 'requests[0]': Cannot find field."
          }
        ]
      }
    ]
  }
}

The fact that it reads "ReplaceImageRequest" shows that it's able to understand my requests object but so what's wrong inside it ?

The API documentation is here.

Thanks for the help


Solution

  • My bad, the request name was not correct. It works with

    let requests = [{
        replaceImage: {
          imageObjectId: "g13fc473bd83_2_0",
          imageReplaceMethod: "CENTER_INSIDE",
          url: "https://pbs.twimg.com/card_img/1553040603776225280/MOtK57RF?format=jpg"
        }
      }]