I'm building a simple spreadsheet that monitors a series of url for their social sharing data using the sharedcount.com API.
For some reason the UrlFetchApp.fetch() eliminate the url I pass as a parameter of the endpoint to interrogate.
This is my code:
var sheet = SpreadsheetApp.getActiveSheet(),
urls = sheet.getRange('A:A').getValues().slice(1),
apikey = "XXXXXXXXXXXXXXXXX",
apiEndPoint = "https://free.sharedcount.com/"
data = [];
urls.forEach(function(url, idx) {
apiurl = apiEndPoint + '?url=' + encodeURIComponent(url) + '&apikey=' + apikey
Logger.log(apiurl)
sharedCountData = UrlFetchApp.fetch(apiurl)
data[idx] = sharedCountData
})
The logger function returns the correctly built url (eg: https://free.sharedcount.com/?url=http%3A%2F%2Fwww.teamvaxitalia.it%2F&apikey=XXXXXXXXXXXXXX), but running my code I receive a 401 error when it reach fetch(), in which the passed endpoint is this: https://free.sharedcount.com/?url=&apikey=XXXXXXXXXXXXXX and the error is "Not a valid URL". The url parameter is missing!
Any idea about what's going on?
Thanks
SOLVED: For some reason with UrlFetchApp.fetch it was needed to use https://free.sharedcount.com/url?url= as endpoint, not just https://free.sharedcount.com/?url=, even if in browser both work.