Can't get screenshot using Google Apps Script

Viewed 418

I am trying to get a screenshot of a website using Google Apps Script from the URL. I was following this guide

I currently have the following code

function snapScreenshot() {
  var siteUrl = 'https://praveen.science/';
  var url = "https://www.googleapis.com/pagespeedonline/v4/runPagespeed?screenshot=true&url=" + encodeURIComponent(siteUrl) + "&key=API_KEY";
  var res = UrlFetchApp.fetch(url).getContentText()
  var obj = JSON.parse(res);
  var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(obj.screenshot.data), "image/png", "sample.png");
  DriveApp.createFile(blob);
}

I currently have this error on var res

Exception: Request failed for https://www.googleapis.com returned code 404. Truncated server response: <!DOCTYPE html>

And if I use var url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url= I am able to get the JSON. I changed my var blob to

var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(obj.lighthouseResult.audits['final-screenshot'].details.data), "image/jpeg", "sample.jpeg"); 

but am getting the error

Exception: Could not decode string.

on var blob

Thanks a lot for your help.

1 Answers

The PageSpeed API is now at version 5 (an earlier version of the API was used in the guide you referenced). Apparently, the latest version of the API no longer supports screenshots (and all earlier versions of the API seem to be disabled/sunset).

Check the PageSpeed API reference. The screenshot query parameter is noticeably absent in the reference documentation.


In the V5 version of the API, screenshots are now directly accessible as a data URI in the JSON response to the runpagespeed endpoint.

JSON response is formatted as follows:

{
    ...
    ...
    "lighthouseResult": {
        ...
        ...
        "audits" : {
            ...
            ...
            "full-page-screenshot" : {
                ...
                ...
                "details": {
                    ...
                    ...
                    "screenshot": {
                        "data": "data:image/jpeg;base64,..."
                    }
                }
            }
        }
    }
}
Related