0

I'm trying to converting a google apps script that I have created into a webapp, and would like to do it in Javascript as it will be easier for me. When I say converting, I mean creating a webapp based on the script.

I have managed to sort out the authentication, however, for some reason I can't seem to get any information from the actual Google sites api. Sites doesn't show up in the api explorer , but shows up as 'Sites v1.4' in the oauth playground, which is really annoying. I have some code which I have been fiddling with:

  <script> function makeApiCall() {
    alert('in makeApiCall');
    var oauthToken = gapi.auth.getToken();
    var url = "https://sites.google.com/feeds/site/mydomain";
    var method = "GET";
    gapi.client.request({
      'path': url,
      'method': method,
      'headers': {
        'authorization': 'Bearer' + oauthToken.access_token
        },
      }).then(function(response) {
        alert('in the then');
        alert(response.status);
      });

  }
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

I get no response from the actual request (the authorization. I have tried using CORS with xhr, but to no avail (i kept on getting a status of 0). I can get a response when using the oAuth playground, and also from the Postman chrome REST client. However, nothing from javascript.

Can someone tell me where I'm going wrong?

Thought I would add my xhr code along with this just in case someone could tell me why I kept on getting a status of '0':

  function callSites() {

          alert('in call sites');
          var data = null;

          var xhr = new XMLHttpRequest();
          var oauthToken = gapi.auth.getToken();
          xhr.withCredentials = true;
          xhr.addEventListener("readystatechange", function () {
            if (xhr.readyState === 4) {
                alert('ready');
                }
          });

          xhr.open("GET", "https://sites.google.com/feeds/site/mydomain");
          alert('opening');
          alert('set request header');
          xhr.setRequestHeader("gdata-version", "1.4");
          xhr.setRequestHeader("authorization", "Bearer " + oauthToken.access_token);
          alert('sending'); 
          xhr.send();
          alert(xhr.status);

  }

Thanks in advance

Update:

I've managed to get past the CORS issue by using ajax with the following code:

function ajaxCallSite() {
    alert('in ajaxCallSite');
    var oauthToken = gapi.auth.getToken();
    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "https://sites.google.com/feeds/site/mydomain",
        "method": "GET",
        "dataType": "jsonp",
        "headers": {
            "gdata-version": "1.4",
            "authorization": "Bearer" + oauthToken.access_token
        }
    }
    alert('before settings function');
    $.ajax(settings).done(function (response) {
    alert(JSON.stringify(response));
    });
  }

However, I'm now getting an access issue. My authorization seems to be working, as I am able to get a token that works. However, it doesn't seem to be feeding through. When I inspect the network request I get a 200 status with 'Not authorized to access this feed', and every now an then I get a 403 with the same response body. Can anyone notice something that I'm missing?

Thanks, again,

Latest Update: Managed to get it fixed. Working code below:

 function ajaxCallSite() {
    alert('in ajaxCallSite');
    var oauthToken = gapi.auth.getToken();
    var settings = {
        "async": true,
          "crossDomain": true,
        "url": "https://sites.google.com/feeds/site/mydomain?access_token=" + oauthToken.access_token,
        "method": "GET",
    "dataType": "jsonp",
        "headers": {
            "gdata-version": "1.4",
            },
        "success": function(response){                          
            alert(response);                   
        }   
    };
    alert('before settings function');
    $.ajax(settings);
  }

Will continue to work on it and update what I have found. Currently trying to make sure the feed brings back all my sites (I have a few), and not just a limited amount.

K Owusu
  • 1
  • 2
  • "I have tried using CORS with xhr, but to no avail (i kept on getting a status of 0)." — How? You don't control `sites.google.com`, so I don't see how you can set CORS headers on it. – Quentin Jun 18 '15 at 13:09
  • "why I kept on getting a status of '0':" — That's the generic "No request made" error. The browser console in the developer tools should give you a more useful error message. – Quentin Jun 18 '15 at 13:10
  • Thanks for your comments. Not quite sure I understand your first comment regarding controlling sites.google.com, but I will be checking dev tools in the browser console to see what comes up. – K Owusu Jun 18 '15 at 13:20
  • Have found out that the status of 0 is linked with CORS not being enabled on the servers I'm using to host my code. I'm in contact with the server admins regarding this, but would prefer not to use gapi.client.request, which is recommended by google. So any help there would be really great. – K Owusu Jun 18 '15 at 17:29
  • Managed to get round it by using ajax and setting the datatype to 'jsonp' according to [this](http://stackoverflow.com/questions/28359730/google-place-api-no-access-control-allow-origin-header-is-present-on-the-req) solution. – K Owusu Jun 23 '15 at 12:56

0 Answers0