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.