Question

I am stuck for 3 days now trying to get 2 google gadgets to reliably communicate on a google site to exchange anonymous user session information based on UiInstance.getId() value. Since gadget-to-gadget pubsub feature is deprecated, I am trying to use the ContentService in the server gadget to service a doPost request from originating from the client gadget - the idea being, I will store the session information in a server gadget and pull the data into gadgets on other pages in the site - not sure if this is possible.

BUT, I am stuck even trying to tinker with the various examples on the ContentService posted on StackOverflow. I am stuck at a very basic step and unable to proceed beyond the super simple server code below. I am not able to figure out what would the URL of the server gadget I need to use from my client gadget. I seem to be getting confusing results.

Server code:

function doGet(e) {
  var output = ContentService.createTextOutput();
  output.setContent("Hi from ContentServer!");
  return output;
}

Steps I followed:

1) Published the server as a web app and set it up so anyone/anonymous can access it.

2) The publish window popup says the gadget is available at https://script.google.com/macros/s/AKfycbyIa07kBC-gqG0nJq3Up5GzUntbTaYcM1KmaYF7vQvFIWN_qxA/exec

3) If I use a browser to open the link, I get "Unknown macro doGet".

4) If I click on the link given by "Test web app for your latest code." from the publish window popup, it seems to take me to https://script.googleusercontent.com/echo?user_content_key=WwuWW_Hhp2wlHpHWsX-qAwu7jtKrwlt1fkkesojKEWeJXKm5AoUOhuRHxu4RIHLrWovOBUsHhOB3No0RYr05RE4tWtliFlXKm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnG5I1NuldcVdfVF6dOROkmljXtr0dWIARhfeRbamxWJIIJeMc5tWnrGPpehwqDtynQ&lib=MfYY3NqJ0IdcpreIfwd3uwgmzn1S_adTp

The above link from step 4 works from a browser BUT its very flaky from my client gadget! It seems to work the very first time I version and publish it but returns raw HTML if I update the version even once.

I believe I am missing some trivial step OR there is a serious version control or deployment bug with web-app gadgets.

I have checked out the following threads on stackoverflow that were pretty useful. How to use ContentService and doPost to create a REST API

KamilG seems to have overcome the issue here Another doGet() issue with Google Apps Script - "Unknown macro doGet" error

But its very wobbly for me and I am unable to pin down any reliable procedure the get the ContentService working from my server gadget.

Any help and explanation will be much appreciated.

Here is my client gadget code.

function doGet(e) {
//  var url = "https://script.google.com/macros/s/AKfycbyIa07kBC-gqG0nJq3Up5GzUntbTaYcM1KmaYF7vQvFIWN_qxA/exec";
  //var url = "https://script.googleusercontent.com/echo?user_content_key=9tzQxTSPm8ADsHKl4bCg6ru-49C19kYZCW8IRuWiWi-Xe_lTlQZS4xq8v5aPqjR6Ybj7-xjjJXyQPhOH0T3KFtJUtXc7U-Dkm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnJlCLn2pnKC0O1TYz53ccgjxtVC9ccM8kFegFJ0AJSK_YP_ylqfjfm1EU_BUbZUu-88PMDJ6xpj8&lib=MTLHdJRs3UPYj3bYgbhBNnNGZ7UmftE34";
  var url = "https://script.googleusercontent.com/echo?user_content_key=LvlwGkBktQ732xyD1HJbh5OhMsuhkidt_rOovw-olm-wJQF4LlErMDSxsfDaAb4UBYqOeTI3-8soIBj-KaOCDi0WlUav4Nl_m5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnG5I1NuldcVdfVF6dOROkmljXtr0dWIARhfeRbamxWJIIJeMc5tWnrGPpehwqDtynQ&lib=MfYY3NqJ0IdcpreIfwd3uwgmzn1S_adTp";

  // WELL ONE OF THE URLS WORKS FROM ABOVE!!! BUT ONLY TILL I UPDATE THE SERVER WEB APP VERSION :(

  var app = UiApp.createApplication().setTitle("ClientService Client test");
  app.setStyleAttribute("background", "azure");

  var fetchMethod = 'get';
  var response = UrlFetchApp.fetch(url, {method: fetchMethod});

  //var lbl = app.createHTML("<b>Response :</b>" + response.getContentText());
  var lbl = app.createTextArea().setText(response.getContentText());

  var lbl1 = app.createHTML("<b>Response code :</b> "
                            + response.getResponseCode()); 

  app.add(lbl);
  app.add(lbl1);
  return app;
}

Help needed please.

Was it helpful?

Solution

This functioning as intended for the most part, but I think there might be some alternatives.

  1. Regarding your point #3 - That URL above works. There must have been something wrong with the version you tested (perhaps you didn't declare doGet correctly?).
  2. The link from #4 is a one time use redirect that happens when you Content Service. See the redirect section here all the way at the bottom. This link is short lived and you might be fast enough to catch it in a browser but a another separate calling client (the "gadget") might not be so lucky. So you should never call the googlecontentservice.com link directly.
  3. Regarding the overall issue of flakiness of ContentService, I have not seen this. If I properly use the /exec link and then properly increment the version after every appropriate change things work as expected for me.

I built a simple web app accessing your Content Service and things seem to work fine for me consistently. See below. The code looks identical to yours but I am using the first url.

https://script.google.com/macros/s/AKfycbw6nlfv5ME6F3Z2WBdW24YAgfQm_E5gYV0xrMDvDwZLrX7PdMU/exec

As an alternative - is there a reason you are doing a server side UrlFetch from one script to another? If you can fetch the data server side, can you just perform the logic of the content script w/in the UI script?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top