I spent a lot of time reading Google manuals and other resources and didn’t figure out what I was doing wrong trying to get a short url using this script:
function test_short_link() {
var apiKey, post_url, options, result;
post_url = "https://www.googleapis.com/urlshortener/v1/url";
apiKey = 'xxx';
post_url += '?key=' + apiKey;
var options =
{ 'method':'post',
'headers' : {'Content-Type' : 'application/json'},
"resource": {"longUrl": "https://google.com/"},
'muteHttpExceptions': true
}
result = UrlFetchApp.fetch(post_url, options);
Logger.log(result);
}
I made various modifications, but it returns:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required",
"locationType": "parameter",
"location": "resource.longUrl"
}
],
"code": 400,
"message": "Required"
}
}
It drives me crazy!
Please help! What is wrong with this code?
source
share