again!
I created an embedded in spreadsheet script that use UrlShortener.Url.insert feature. My client wants to be able to make a new instances of this spreadsheet to share it with colleagues. I've implemented this feature, but when I begin to test new instance it's turned out that I have to enable URL Shortener API in my Google Developers Console.
I wonder if I can bypass this manual work using my script or only I can do is to provide client with instructions how to do it?
Update: Sandy Good recomends to use UrlFetch.fetch() to get the short link, but this code:
function test_short_link() {
var options =
{
'longUrl': 'http://www.google.com/',
'muteHttpExceptions': true
};
var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
options);
Logger.log(result);
}
returns this:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: shortUrl",
"locationType": "parameter",
"location": "shortUrl"
}
],
"code": 400,
"message": "Required parameter: shortUrl"
}
}
Looks like this topic
And this code
function test_short_link() {
var options =
{
'longUrl': 'http://www.google.com/',
'muteHttpExceptions': true,
'method':'post'
};
var result = UrlFetchApp.fetch("https://www.googleapis.com/urlshortener/v1/url",
options);
Logger.log(result);
}
bring us:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "userRateLimitExceededUnreg",
"message": "User Rate Limit Exceeded. Please sign up",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "User Rate Limit Exceeded. Please sign up"
}
}
Edit: No there is no way to access the Google Dev console through any method beside the website itself.
Here is the method to do this by UrlFetchApp. You need to pass the options in payload parameter, not in the UrlFetchApp options object. You also need to pass the current users OAuth token in the header. Of course you will need to modify this code as it hard codes the longUrl and does no error checking.
function ShortenUrl(){
var url = "https://www.googleapis.com/urlshortener/v1/url"
var payload = {"longUrl":"www.google.com"};
var parameters = { method : 'post',
headers : {'Authorization': 'Bearer '+ScriptApp.getOAuthToken()},
payload:JSON.stringify(payload),
contentType:'application/json',
muteHttpExceptions:true};
var response = UrlFetchApp.fetch(url, parameters);
Logger.log(response);
}