Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Querying Google spreadsheet: No 'Access-Control-Allow-Origin' header is present

I'm trying to use a Google spreadsheet to record data that is entered along the way in my Twine story. I can use the Google Forms paradigm to add new entries to a spreadsheet, but I'd like to be able to load that spreadsheet and pre-populate previous values into the game on a per-user basis, and I'd like to do updates to existing rows in the spreadsheet. However, when I try to use direct access to the spreadsheet (rather than via an associated form), I always get this error:

"No 'Access-Control-Allow-Origin' header is present....."

I've shared the spreadsheet globally; tried various permutations of the URL, but nothing seems to work and I can't find much about it on the web, at least that I comprehend.

Here's the kind of JS I'm trying out in a Script passage:

window.queryGoogle = function() {

var json_uri = "https://docs.google.com/spreadsheets/d/1WhSTBdJnL91T9GnXv3f8p8Hgv_-sYq5lpw5mn0L_Re0/gviz/tq?&tq=&gid=0&usp=sharing";

  $.ajax({
    type: "GET",
    url: json_uri,
    xhrFields: { withCredentials: true },
  datatype: "jsonp",
  success: function(data){
      alert(JSON.stringify(data));
    }
  });
        };

Has anyone done this kind of thing recently with the Google spreadsheets (or other doc types)?

Comments

  • I think your main problem may be that the URL you are using will not return a JSON string. (I'm not 100% sure of that, since access via that URL is restricted, which probably also means that it will not work as you intend unless you happen to be logged into the account in the same browser where you are testing.) When I have done this sort of thing, I have published the document and modified the URL for the ATOM feed to get this sort of URL:

    https://spreadsheets.google.com/feeds/list/1WhSTBdJnL91T9GnXv3f8p8Hgv_-sYq5lpw5mn0L_Re0/od6/public/values?alt=json-in-script

    Both of the boldfaced portions of the URL can vary depending on the spreadsheet.
  • Another (possibly overkill) library for accessing Google Spreadsheets: http://techcrunch.com/2014/10/06/stamplay-is-ifttt-for-back-end-development/
  • The 'No Access-Control-Allow-Origin header' message is generally a result of your resource (game/story) being on one domain (website) and the spreadsheet resource your trying to access being on another, known as Cross-site HTTP requests (or CORS) as explained here.

    You will need to determine why google is not returning an Access-Control-Allow-Origin in there response to you, if that is actually the case. I'm not at a machine currently where I can do that for you.
Sign In or Register to comment.