-->

Google Apps Script RSS Output

2020-07-27 04:29发布

问题:

I am trying to use Google Apps Script to output a formatted RSS feed of upcoming events from a calendar that I can then insert into a Mailchimp Template.

This is the HTML I am using to generate the rss feed:

<rss version="2.0">
<channel>
  <title><?= "Title" ?></title>
  <?   var cal = CalendarApp.getCalendarById('CalendarID');
       var today = new Date();
       var future = today.addDays(90); //Uses separate function to add days to look ahead
       var events = cal.getEvents(today, future);
     for (var t in events) { ?>
     <item>
        <? var start = new Date(events[t].getStartTime())
           var date = Utilities.formatDate(start, 'GMT', 'dd/MM/yyyy')
           var title = events[t].getTitle()
           var time = start.toTimeString().substring(0, 5) ?>
       <title><?= date+' - '+time ?></title>
       <description><?= title ?></description> 
     </item>
  <? } ?> 
</channel>
</rss>

This is the doGet() script the URL calls:

function doGet() {
  return ContentService.createTextOutput(HtmlService.createTemplateFromFile("rss").evaluate().getContent())
          .setMimeType(ContentService.MimeType.RSS);
}

But whenever I pass it through feedburner or any other RSS Reader it comes up with the same response:

The URL does not appear to reference a valid XML file. We encountered the following problem: Error on line 921: The element type "meta" must be terminated by the matching end-tag ""

Even though the call loads fine in a normal browser window as a RSS feed.

I have also the tired the GAS RSS example using the XKCD feed (found here) and I am getting same response.

Any thoughts on what I am doing wrong I have set the web app permissions to Execute as me and Anyone, even anonymous. As said the in the example. Have tried other combinations with no luck as well.

Thanks

回答1:

The problem, as far as I can tell is this:

For security reasons, content returned by the Content service isn't served from script.google.com, but instead redirected to a one-time URL at script.googleusercontent.com. This means that if you use the Content service to return data to another application, you must ensure that the HTTP client is configured to follow redirects.1

The script works on browsers (Firefox, Opera, etc), but no RSS reader recognises it. So, there, not sure how to get around it, but you're half way there.