1

I would like to use an API (url shortener) with a public google Add-on. For the moment my code returns:

Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

  • Is this possible?
  • If yes, Do I need an authentification Token?
    • If yes, what key type should I choose?
    • How can I implement the authorisation for this kind of use?
    • Do I need to pay for it?
  • If no, How could other add-ons use external APIs

Thanks a lot for your answers,

Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
JSmith
  • 4,519
  • 4
  • 29
  • 45
  • Are you familiar with the Google Add-On group? [Google Community](https://plus.google.com/u/0/communities/117193953428311185494) – Alan Wells May 24 '15 at 23:46
  • Well I have been told to ask questions on stack overflow ??so What else can I do with this group? – JSmith May 25 '15 at 00:08
  • The Google documentation *does* direct people to stackoverflow, which gives people the impression that stackoverflow is the place for any general question. And then when people ask a general question, they get down votes. Your question is about Add-Ons, and that group is specifically for Add-Ons. – Alan Wells May 25 '15 at 00:21
  • Ok well thank you Sandy I'll find my answer on this group, but even if it's a pretty general question, I felt like it could help people understanding the basic of API inplementation on various plans. Thanks anyway :) – JSmith May 25 '15 at 00:26
  • You may still get an answer here. There is the "official" way that stackoverflow is supposed to work, and then there's reality. – Alan Wells May 25 '15 at 00:41
  • The daily free quota is 1,000,000 requests. How are your set up? Are you using the Url Shortener Advanced service with the api enabled in the dev. console? – Spencer Easton May 25 '15 at 20:12

1 Answers1

1

EDIT: The OP pointed out in the comments that this is a custom function. Custom function run with limited authorization. A complete list of what is available is at:
https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

Below uses the REST API to get the shortened url. This will work with custom functions. You will just need to enable the URL Shortener API and generate a Server API Key. Use the IPs at the following link for your server api key:
https://developers.google.com/apps-script/guides/jdbc#setup_for_google_cloud_sql

/**
 * Returns a shortened URL of the input.
 *
 * @param {string} longUrl The long URL to shorten.
 * @return The shortened url.
 * @customfunction
 */
function getShortUrl(longUrl) {

   var payLoad = {"longUrl": longUrl};
   var apiKey = PropertiesService.getScriptProperties().getProperty("ServerApiKey");
   var url  = "https://www.googleapis.com/urlshortener/v1/url?key="+ apiKey;
  var options = { method:"POST",
                 contentType:"application/json",
                 payload:JSON.stringify(payLoad),
                 muteHttpExceptions:true};

  var response = UrlFetchApp.fetch(url, options);
  if(response.getResponseCode() != 200){throw new Error("Unable to shorten url");}
  return JSON.parse(response).id;
}

Original Post


Here is a quick primer on using the UrlShortener advanced service. You will need to turn on the service and activate the Url Shortener api in the developers console. This will give you a quota of 1,000,000 requests per day for your add-on.

function myFunction() {
  var url = UrlShortener.newUrl();
  url.longUrl = "http://www.example.org";  
  var short = UrlShortener.Url.insert(url);
  Logger.log(short);

  //list all users shortened urls
  Logger.log(UrlShortener.Url.list());
}
Spencer Easton
  • 5,642
  • 1
  • 16
  • 25
  • well everything works fine when I use it inside the IDE but I cannot use it as custom function. Is it related to the authorization of my app? Here is the message I get "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." thanks in advance – JSmith May 27 '15 at 00:03
  • Ah custom functions are a different beast. Ill edit the answer. – Spencer Easton May 27 '15 at 03:22
  • Thanks, because I've dealt already with trigger issues but still don't completely get it – JSmith May 27 '15 at 03:36
  • Ok many thanks, it works now. But i still didn't make it go live. – JSmith May 28 '15 at 00:23