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.